2

I'm developing an app with expo (SDK 48). I'm trying to listen to notifications received in the background on an IPhone 11 (real device).

I already added this to my App.js file:

const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';

TaskManager.defineTask(
  BACKGROUND_NOTIFICATION_TASK,
  ({ data, error, executionInfo }) => {
    if (error) {
      report.log("error occurred");
    }
   
      console.log('Notification in background received: ' + JSON.stringify(data))
      let newNotification = new MyNotification(
        getFormattedDate(new Date()),
        data?.notification.data.title || 'Title',
        data?.notification.data.message || 'Message'
      )
      saveNotification(newNotification).then(() => store.dispatch(setNewMessageStatus(true))).catch(err => console.log('Error saving notification ' + err));
    
  }
);

Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);

Expo Notification documentation says: "To handle notifications while the app is backgrounded on iOS, you must add remote-notification to the ios.infoPlist.UIBackgroundModes key in your app.json, and add "content-available": 1 to your push notification payload. Under normal circumstances, the "content-available" flag should launch your app if it isn't running and wasn't killed by the user, however, this is ultimately decided by the OS, so it might not always happen."

This is my "ios" key in my app.json:

 "ios": {
      "supportsTablet": true,
      "googleServicesFile": "./GoogleService-Info.plist",
      "bundleIdentifier": "com.gsjardim83.foodfreedomRN",
      "infoPlist":{
        "UIBackgroundModes":["remote-notification"]
      }
    },

But I cannot figure out how to set "content-available": 1 using the expo push api.

My listener is called when app is in foreground only, but not in the background. When I check Apple's documentation, they give a bit more details as to where the content-available key should go: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

Is there a way to do that through the expo push api? I would like to avoid having to use APN server to keep things simple.

I tried to send the following request to expo push api:

{
        "to": "ExponentPushToken[xxxxxxxxxx]",
        "sound": "default",
        "body": "Message in the background 3",
        "badge": 1,
        "title": "Food freedom App",
        "priority": "high",
        "data": {
            "trigger": {
                "payload": {
                    "aps": {
                        "content-available": 1
                    }
                }
            }
        }
    }

But what I received was (I was only able to see this when I interacted with the notification, because the onReceivedListener doesn't get called):

{
   "actionIdentifier":"expo.modules.notifications.actions.DEFAULT",
   "notification":{
      "date":1679656100.9144022,
      "request":{
         "content":{
            "summaryArgumentCount":0,
            "targetContentIdentifier":null,
            "threadIdentifier":"",
            "attachments":[
               
            ],
            "categoryIdentifier":"",
            "summaryArgument":null,
            "data":{
               "trigger":{
                  "payload":{
                     "aps":{
                        "content-available":1
                     }
                  }
               }
            },
            "title":"Food freedom App",
            "subtitle":null,
            "badge":1,
            "launchImageName":"",
            "sound":"default",
            "body":"Message in the background 2"
         },
         "identifier":"CD4C4301-3696-4FA6-A080-333E59B42A8B",
         "trigger":{
            "payload":{
               "body":{
                  "trigger":{
                     "payload":{
                        "aps":{
                           "content-available":1
                        }
                     }
                  }
               },
               "aps":{
                  "sound":"default",
                  "badge":1,
                  "alert":{
                     "body":"Message in the background 2",
                     "launch-image":"",
                     "title":"Food freedom App",
                     "subtitle":""
                  },
                  "category":"",
                  "thread-id":""
               },
               "scopeKey":"@gsjardim83/foodfreedomRN",
               "projectId":"xxxxxxxxxx",
               "experienceId":"@gsjardim83/foodfreedomRN"
            },
            "type":"push",
            "class":"UNPushNotificationTrigger"
         }
      }
   }
}

I've tried other formats too, but the content-available doesn't show under the "aps" key of the "payload"

Gil Jardim
  • 21
  • 4

1 Answers1

2

You can add _contentAvailable: true to your payload so it looks something like this: { to: <push_token>, "_contentAvailable": true}

Riley MacDonald
  • 453
  • 1
  • 4
  • 15