expo: [expo-notifications] Background notification tasks not working
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
- 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
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 45 (7 by maintainers)
Commits related to this issue
- Add _contentAvailable flag for iOS background notifications Please refer to notifications documentation here https://docs.expo.dev/versions/latest/sdk/notifications/#background-events Related issue ht... — committed to mgscreativa/expo-server-sdk-php by mgscreativa a year ago
- Add _contentAvailable flag for iOS background notifications Please refer to notifications documentation here https://docs.expo.dev/versions/latest/sdk/notifications/#background-events Related issue ht... — committed to mgscreativa/expo-server-sdk-php by mgscreativa a year ago
Expo’s push API does not accept the
content-availablekey. You’ll need to send directly through APNs. Please refer to the Push API documentation: https://docs.expo.dev/push-notifications/sending-notifications/Hi Guys, I found out a solution in order to use content-available: 1 flag in expo server sdk. Just use
"_contentAvailable": truein your payload Example payload for ios{ to: <push_token>, "_contentAvailable": true}Example payload for android
{ to: <push_token>, priority: "high"}Just use the above payload. For me it worked like a charm
Hi! I confirm that the background task works on Android but not on iOS. I’m on SDK 45 and using a development build.
This is my notification payload in PHP
My setupNotificationsBackgroundTask.js hook
app.config.js
If the ‘content-available’ => 1 parameter is not supported by the Expo API message payload, then, please state that clearly in the docs, because currently it states:
Please note: In order 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.
And maybe it should add: and add “content-available”: 1 to your push notification payload, currently only supported by native APNs payloads, not supported in Expo notifications API message payloads
Please advise.
@cruzach So if I am seeing the following error when running my app via Expo Go (managed), is this expected? I have the proper plist key & value
UIBackgroundModes: ['remote-notification'],and have tried both killing & reinstalling the expo go app but still see the same Error message.If backgrounded remote notification handling & async task definitions only work when running the app itself outside of Expo Go, how do you recommend debugging the async task code?
–
And furthermore, 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?
@cruzach are you please able to confirm that this is still the case, and if possible could you please explain why Expo’s push API does not accept the
content-availablekey? (and will it ever accept this key?)We are trying to update the iOS badge count with the background task following the docs, and the docs simply say Please note: In order 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.
The docs don’t confirm that you would then have to go through APNs directly, and this is the only place I can find where
content-availableis mentioned.If I could please have some confirmation that we would need to switch to use the native device token instead of the expo push token, and send directly to APNs, then I can communicate with the backend team and project manager that we MUST do this and allocate our resources accordingly.
Thank you 🙏
Unbelievably this is working for us. Using
BACKGROUND-NOTIFICATION-TASKto update our badge count. @ahmed-sharief5 how did you find this solution?Maybe we should upvote this https://expo.canny.io/feature-requests/p/background-notification-support-for-expo-notifation-service-api
I created a feature request.
https://expo.canny.io/feature-requests/p/background-notification-support-for-expo-notifation-service-api
@chris-chao any luck in past 20 days? I’m on SDK 42 My code is similar to what is on the official documentation.
Not working whether app is running in background or killed. I’m on Android device btw.
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-clientlibraryIf you’re using APNs directly, please refer to the APNs docs for where to place keys
Agreed about adding it. It feels like a pretty low lift, no? Maybe I’m missing something about the complexity given how common of a use case it is
I spoke with someone at Expo and when I asked about the content-available flag, this was the response:
It also sounded like it wasn’t on the 2022 roadmap either.
We’ve since migrated to AWS’ SNS for iOS pushes
Hello @jcalem @brentvatne
I am having an issue with updating the state ( context API) from inside the TaskManager ( as it’s defined in the global scope), first of all, is it possible to do so ? if the answer is Yes, could you please give me an example on how to implement this. Thank you.
I read this wrong. You can still receive notifications if the App is backgrounded or closed. You just can’t directly act on them using code in the app in those situations. I am guessing here, but if you touch the notification, I would imagine you can still act on it after the app opens, correct?
Hello,
I did totally the same, but on iOS, I have this warning : " Error: Background remote notifications have not been configured. To enable it, add
remote-notificationtoUIBackgroundModesin the application’s Info.plist file.]".In any case, in the documentation they say: “Handling incoming notifications when the app is not in the foreground (not supported in Expo Go)” but here: https://blog.expo.dev/expo-sdk-42-579aee2348b6 they say “SDK 42 brings support for two highly-requested expo-notifications features: handling push notifications while your app is in the background, and using custom notification sounds in your managed workflow app”, where is the truth?
Thanks.
This is work, but background will run just one time only till your re-open the app.
@jamespb97
@dwbelliston I found out this in official documentation of firebase. Does it works for you after disabling “Do not disturb”?
Hi how you doing? If by expo dev build, you mean a custom build then yes, I used eas and built it really easy, I used the rn-firebase library and it has eas support out of the box so really easy to setup.
Regarding your second question FCM is completely free, as a matter of fact expo notification (for Android only) is using the FCM.
That’s bad news, we also migrated to FCM and surprisingly this feature was supported out of the box and without any problems, took me a day to set it up and make it running.
Do most Expo developers reply on sending notifications directly through APNs for iOS instead of using Expo’s push notification system?
I suppose I’m just surprised this isn’t a bigger issue that it’s currently not possible using Expo’s Push API to increment the app badge count when receiving notifications.
Maybe I’m still misunderstanding? @Stevemoretz
As we state in the docs:
I’ve seen this myself, and what helped was to fully kill all backgrounded apps, relaunch my app, and then try sending the notification again. You should also make sure “Background App Refresh” is allowed for your application in Settings