I'm trying to implement background push notifications in my Flutter app. The goal is to trigger a process in the background when a push notification arrives, and as a proof of concept, I just want to log something to confirm the device is receiving the notification. The device receives alert push notifications normally, but I don't see any logs or responses when a background push notification (a type of push notification with "content-available": 1) arrives, whether the app is in the foreground, background, or terminated.
Here’s what I have so far:
override func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
// Log to confirm that background notification was received
NSLog("Background push notification received.")
// Attempt to invoke Flutter method to handle the notification
do {
let someData = "Notification data received" // Example data you want to pass
self.channel?.invokeMethod("backgroundNotificationArrived", arguments: someData) { (result) in
let resultDescription = String(describing: result)
completionHandler(.newData) // Indicate that new data was received
NSLog("Flutter method backgroundNotificationArrived invoked, response: \(resultDescription)")
}
} catch {
// Handle any errors and log the failure
NSLog("Error invoking Flutter method or serializing userInfo: \(error.localizedDescription)")
completionHandler(.failed) // Indicate failure in processing
}
}
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
</array>
{
"aps": {
"content-available": 1
},
"custom-data": "true"
}
Questions:
I was following this article to send the background push notification:
Thank you in advance for any suggestions or insight!