我已经尝试了几天了,但我无法弄清楚。 Firebase Messaging 插件不会调用在 onBackgroundMessage
中设置的函数,即使应用程序处于后台(且未终止)也不调用。我已经在调试和发布版本中尝试过这个,但没有。
我正在尝试通过 flutter_local_notifications
发送通知,但我认为这并不重要,因为甚至没有调用回调。
我确定我的应用已连接到 Firebase,因为它确实在 onMessage
中收到通知。
我做过的事情:
1) 通过 Firebase 控制台连接应用,将 GoogleService-Info
添加到应用。
2) 将 APNs 授权 key 上传到 Firebase 控制台。
3) 将此功能作为顶级功能添加到我的应用中:
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) 确保我的应用启用了推送通知和后台模式(在所有构建中):
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 版本,它在 Debug模式下对我有用 所以请仔细检查此步骤:
- 确保从 xcode 添加后台和远程通知功能
- 不要使用 firebase 控制台来测试后台通知,因为它不会工作
- 构建小型服务器或使用 postman 发送通知,您可以在 firebase 控制台文档中找到 https://firebase.google.com/docs/cloud-messaging/send-message#node.js 或者使用这个 repo https://github.com/SHAHIDNX/NODEJS_pushnotifications
- 发送静默通知
{ "to": "fcm_token", "data": { "clear_data": true }, "content_available": true, "apns-priority": 5 }; notitce the different in content available
关于ios - Firebase Messaging 不会在 iOS (11.4.1) 上调用 BackgroundHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72583264/