admin管理员组

文章数量:1410688

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:

  1. In AppDelegate.swift:
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
  }
}

  1. In Info.plist:
<key>UIBackgroundModes</key>
<array>
  <string>fetch</string>
  <string>location</string>
  <string>processing</string>
  <string>remote-notification</string>
</array>
  1. Sending the Body from Apple Developer Push Notifications Platform:
{
  "aps": {
    "content-available": 1
  },
  "custom-data": "true"
}
  1. Flutter Code (simplified for context): I’m using Flutter with Swift code to handle the background notifications. The method backgroundNotificationArrived is supposed to be triggered when the background push notification arrives. However, the notification doesn’t seem to be triggering or logging anything.

Questions:

  1. Is there anything I'm missing in the setup to ensure the app wakes up to process background push notifications when the app is in the background or terminated?
  2. What debugging steps should I take to verify that the background fetch is being triggered, especially when the app is in the background or terminated?

I was following this article to send the background push notification:

Thank you in advance for any suggestions or insight!

本文标签: