question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[expo-notifications] Background notification tasks not working

See original GitHub issue

Summary

I followed the expo notification here, but have not been unsuccessful getting the background notification task to be performed. I send a push notification via: curl --location --request POST 'https://exp.host/--/api/v2/push/send' \ --header 'Content-Type: application/json' \ --data-raw '{ "to": "ExponentPushToken[***************]", "title":"Testing", "body": "Testing body", "content-available": 1, "data": { "type": "testType" } }' The notification is received but the background task is not performed. In the device logs I can see the background task is registered (I’m testing on an iPhone 11 on a TestFlight build):

Jul 25 14:28:04 ComBingedapp(UserNotifications)[35196] <Notice>: [com.bingedapp] Creating a user notification center
Jul 25 14:28:04 ComBingedapp[35196] <Notice>: EXTaskService: Restoring tasks configuration: {
    "@jcalem/Binged-app" =     {
        appUrl = "https://expo.io:443/@jcalem/Binged-app";
        tasks =         {
            "BACKGROUND-NOTIFICATION-TASK" =             {
                consumerClass = EXBackgroundRemoteNotificationConsumer;
                consumerVersion = 0;
                name = "BACKGROUND-NOTIFICATION-TASK";
                options =                 {
                };
            };
        };
    };
}

And the notification is displayed:

Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Received remote notification request 98FF-3C16 [ waking: 0, hasAlertContent: 1, hasSound: 0 hasBadge: 0 hasContentAvailable: 0 hasMutableContent: 0 pushType: Alert]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Process delivery of push notification 98FF-3C16
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Error>: No data found at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/Categories.plist
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Badge can be set for notification 98FF-3C16: 0 [ canBadge: 1 badgeNumber: (null) ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: Getting effectiveSectionInfo for section identifier: com.bingedapp
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Getting effective section info
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: Getting requested effective section info (sync)
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: Got effective section info [ result: 0 ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Got effective section info [ hasResult: 1 ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: Getting effectiveSectionInfo for section identifier: com.bingedapp
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Getting effective section info
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: Getting requested effective section info (sync)
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: Got effective section info [ result: 0 ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Got effective section info [ hasResult: 1 ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [Persisting notification 98FF-3C16: 1 [applicableSettings: 1]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Saving notification 98FF-3C16: 1 [ hasAlertContent: 1, shouldPresentAlert: 1 ]
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: Saving file at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/DeliveredNotifications.plist with 16 items
Jul 25 14:28:15 SpringBoard(CoreServicesInternal)[63] <Notice>: kExcludedFromBackupXattrName set on path: <private>
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Delivered user visible push notification 98FF-3C16
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Error>: No data found at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/PendingNotifications.plist
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Load 0 pending notification dictionaries
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Error>: No data found at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/Categories.plist
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Notice>: [com.bingedapp] Adding notification 98FF-3C16 [ hasAlertContent: 1, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]; destinations 270: (
    NotificationCenter,
    LockScreen,
    Alert,
    Forwarding
)
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Error>: No data found at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/Categories.plist
Jul 25 14:28:15 SpringBoard(UserNotificationsServer)[63] <Error>: No data found at /var/mobile/Library/UserNotifications/0F01E0E1-A979-4946-BEDC-12EC59173DDD/Categories.plist
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: BBDataProviderProxy com.bingedapp has enqueued a bulletin request
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: BBDataProviderProxy com.bingedapp is now sending enqueued bulletin request to BBServer
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: BBDataProvider: Add bulletin to destinations 270: (
    NotificationCenter,
    LockScreen,
    Alert,
    Forwarding
)
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: Publishing bulletin for section <com.bingedapp>: subsectionIDs: (null), bulletinID = <AFABFBA5-9B4C-445F-BA9F-921EF713184B>, expiration date = <Sun Aug  1 14:28:15 2021>, expiration events <0>
Jul 25 14:28:15 SpringBoard(BulletinBoard)[63] <Notice>: BBServer: Publish bulletin AFABFBA5-9B4C-445F-BA9F-921EF713184B to feeds 1067: (
    NotificationCenter,
    LockScreen,
    Banner,
    LockedBanner,
    Forwarding
)

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

SDK Version (managed workflow only)

42

Environment

Expo CLI 4.4.7 environment info: System: OS: macOS 11.2.3 Shell: 3.2.57 - /bin/bash Binaries: Node: 12.13.0 - /usr/local/bin/node Yarn: 1.19.1 - /usr/local/bin/yarn npm: 7.11.2 - /usr/local/bin/npm Managers: CocoaPods: 1.8.3 - /Users/jcalem/.rvm/rubies/ruby-2.6.3/bin/pod SDKs: iOS SDK: Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4 IDEs: Android Studio: 3.5 AI-191.8026.42.35.6010548 Xcode: 12.5/12E262 - /usr/bin/xcodebuild npmPackages: expo: ^42.0.0 => 42.0.3 react: 16.13.1 => 16.13.1 react-dom: 16.13.1 => 16.13.1 react-native: https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz => 0.63.2 react-native-web: ~0.13.12 => 0.13.18 npmGlobalPackages: expo-cli: 4.4.7 Expo Workflow: managed

Reproducible demo or steps to reproduce from a blank project

  1. Add remote-notification to UIBackgroundModes:
"infoPlist": {
   ...
   "UIBackgroundModes": [
       "remote-notification"
   ]
}
  1. Define background task (globally scoped)
const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';

TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({ data, error, executionInfo }) => {
    // Update state
});

I’ve been able to get BackgroundFetch working with the same task body so I know this part works 3. Register the task in a top level component

...
useEffect(() => {
   Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
}, [])
...
  1. Add “content-available”: 1 to push notification payload

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:30 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
cruzachcommented, Aug 31, 2021

Expo’s push API does not accept the content-available key. You’ll need to send directly through APNs. Please refer to the Push API documentation: https://docs.expo.dev/push-notifications/sending-notifications/

3reactions
cruzachcommented, Aug 10, 2021

The docs explicitly state “Handling incoming notifications when the app is not in the foreground (not supported in Expo Go)”. You can test that behavior by either running expo run:ios, building your standalone app, or by using the expo-dev-client library

when the documentation mentions including “content-available”: 1 in the push notification payload, does that mean to include it in the data key as below?

If you’re using APNs directly, please refer to the APNs docs for where to place keys

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expo-notifications background notification reception handling
Even After doing all this, When a notification arrives this task is not running and I am not getting any output in the...
Read more >
Notifications - Expo Documentation
When a notification is received while the app is backgrounded, using this function you can set a callback that will be run in...
Read more >
Background tasks and local notifications : r/expo - Reddit
I'm using the expo background task and notification modules. I've successfully got local notifications to emit. But not from background task ...
Read more >
Basics and caveats of Expo-notifications - DEV Community ‍ ‍
Expo -notifications allows to receive notifications when the app is in the foreground, background and killed. Be aware that you must define to ......
Read more >
Background notification handling | Voters - Expo - Canny
Has someone a working example for react native navigation, currently it seems not to work for eas builds if the app was killed,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found