繁体   English   中英

Firebase 消息不会在 iOS (11.4.1) 上调用 BackgroundHandler

[英]Firebase Messaging won't call BackgroundHandler on iOS (11.4.1)

我已经尝试了几天,但我无法弄清楚。 Firebase 消息插件不会调用在 onBackgroundMessage 中设置的onBackgroundMessage ,即使应用程序处于后台(且未终止)也不会调用。 我已经在调试和发布版本中尝试过这个,但没有。

我正在尝试通过flutter_local_notifications发送通知,但我认为这并不重要,因为甚至没有调用回调。

我确定我的应用已连接到 Firebase,因为它确实onMessage中收到通知。

我做过的事情:

1)通过Firebase控制台连接应用,在应用中添加GoogleService-Info

2)将 APNs 授权密钥上传到 Firebase 控制台。

3)将此 function 作为顶级function 添加到我的应用程序中:

  Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // Not called in background\terminated state
  print("Handling a background message: ${message.messageId}");
  ...
  }

在 main() 中:

 FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

4)将以下行添加到 AppDelegate:

import UIKit
import Flutter
import Firebase
import FirebaseMessaging

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

   if #available(iOS 10.0, *) {
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
   }
  
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

}

5)确保我的应用程序启用了推送通知和后台模式(在所有版本中):

在此处输入图像描述

6)授予应用程序通知权限: 在此处输入图像描述

7)使用node.js admin SDK发送如下通知(没有通知标签,只有数据标签):

let message = {
    apns: {
        headers: {
            'apns-priority': '5',
        },
        payload: {
          aps: {
            contentAvailable: true
          },
        },
      },
    android: {
        priority: 'normal',
    },
    
    data: {
        title: "Test",
        message: "Test",
        url: "https://www.google.com/"
    },
    topic: topic
};

在 Mac 上使用控制台应用程序进行调试会产生以下结果:

default 20:48:06.069282+0300    SpringBoard Received incoming message on topic com.matkonit at priority 5
default 20:48:06.082948+0300    SpringBoard [com.matkonit] Received remote notification request 3823-43DB [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background]
default 20:48:06.083005+0300    SpringBoard [com.matkonit] Process delivery of push notification 3823-43DB

因此操作系统确实收到了通知,但未调用 Firebase 处理程序。


我还尝试了其他 StackOverlow 帖子中建议的无数解决方案,但没有任何帮助......

任何帮助,将不胜感激。

我有以下 firebase 版本,它在调试模式下对我有用,所以请仔细检查以下步骤:

  1. 确保从 xcode 添加后台和远程通知功能
  2. 不要使用 firebase 控制台来测试后台通知,因为它不会工作
  3. 构建一个小型服务器或使用 postman 发送通知,您可以在 firebase 控制台文档https://firebase.google.com/docs/cloud-messaging/send-message#node.js中找到或使用此 repo https:// github.com/SHAHIDNX/NODEJS_pushnotifications
  4. 发送静默通知

{“to”:“fcm_token”,“data”:{“clear_data”:true},“content_available”:true,“apns-priority”:5}; 注意可用内容的不同

有什么解决办法吗? 我面临着类似的问题。

在应用程序委托中,您是否尝试过检查 launchOptions 是否包含“UIApplicationLaunchOptionsRemoteNotificationKey”。 一旦我没有在我的应用程序中收到通知,因为它被附加在启动选项中,所以我必须在那里检查和处理它们。 不确定它是否会有所帮助,但它是一个边缘案例。

https://developer.apple.com/documentation/uikit/uiapplicationlaunchoptionsremotenotificationkey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM