Read a Push Notification using Expo from a cold start (when app is killed)

Viewed 4200

I have an Expo standalone app and I am sending Push Notifications from my server. I have a notification listener in my App.js componentDidMount like so.

componentDidMount = () => {
  this._notificationSubscription = Notifications.addListener(this._handleNotification);
}

The handler gets called when my app is running in the foreground or background.

However, when my app is not running (not even in background) and the notification is delivered, on tapping the notification the app opens but the handler is not called.

It seems to me that the listener registration is "too late".

My notification has a useful payload that I want to use in the app, but unless the app is running, it is lost in this "cold start" case.

Is there a way to preserve this notification data on cold start and call this listener?

3 Answers

In expo SDK 42 this is how we run tasks when notification is received while app is in background.

import * as TaskManager from 'expo-task-manager';
import * as Notifications from 'expo-notifications';

const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';

TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({ data, error, executionInfo }) => {
  console.log('Received a notification in the background!');
  // Do something with the notification data
});

Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);

Expo now only doesn't have any listeners for when the app is killed. enter image description here

There is currently no way to read a Push Notification from a "cold start" using Expo.


Push Notifications are surprisingly difficult to work with.

There are 3 different states your app can be in, and on top of that, users could choose to – or not to – interact with the received Push Notification in any of these states:

  1. Foreground - your app is open, and currently on the user's screen.
  2. Background - your app is open, and hidden, or the device is locked.
  3. "Killed" - your app is not open at all, or is in the background and has not been used in a while.

Expo handles the following:

              Foreground    Background   Killed
-------------------------------------------------
   Received    ✅ Yes        ❌ No        ❌ No
 Interacted    ✅ Yes        ✅ Yes       ❌ No

There is an open issue for Expo SDK 38 here: https://github.com/expo/expo/issues/6943

I will do my best to keep this answer up to date if/when the Expo team has found a workaround, or fixed this issue.

It seems after Expo 38 upgrade at least it is now consistent.

  1. If the notification is triggered while the app is in background, it’s presented immediately the application is not informed of this

  2. If the notification is triggered while the app is in foreground, the application not only receives immediate information about this, but… it can also decide at runtime whether to display a heads-up alert or play a sound! (For more information see the Notification Handler documentation.)

Related