[expo-notifications] Background notification tasks not working
See original GitHub issueSummary
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
- Add remote-notification to UIBackgroundModes:
"infoPlist": {
...
"UIBackgroundModes": [
"remote-notification"
]
}
- 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);
}, [])
...
- Add “content-available”: 1 to push notification payload
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:30 (6 by maintainers)
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/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 theexpo-dev-client
libraryIf you’re using APNs directly, please refer to the APNs docs for where to place keys