最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Flutter Background Push Notification: Not Receiving Notification or Logging in AppDelegate.swift - Stack Overflow

programmeradmin3浏览0评论

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论