I’m struggling to find references that discuss how exactly to structure my message .json
s for background-notifications in FCM. I have tried various configurations, but right now I have this:
const message = {
notification: {
title: "Title",
},
apns: {
payload: {
aps: {
'contentAvailable': 1
}
},
},
token: registrationToken,
};
I want this message to show up as a notification–i.e. for the user to see it–but I also want to trigger:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
So I can perform data updates if the app is in background mode. I am able to see this notification, but that function isn’t triggered when the notification arrives–so no data refresh is possible. How can I format my FCM .json
to enable this?
2
Answers
First thing, according to apple documentation, the key
contentAvailable
that you are using should actually becontent-available
, so without fixing this, theapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
function wouldn’t be triggered anyway.Now, FCM have an example of how the
.json
file should look like, so for your data it should be something like this:Now I guess you’d like to customize the notification a bit more (for example adding body, subtitle etc.), so you may find the full list of keys organized in tables in the article Generating a Remote Notification.
You got payload and headers, payload should contain content_available = 1(not contentAvailable, other than that you are fine) and headers should have "apns-push-type" = "background" and "apns-priority" = "5"
Apple Documentation