我知道之前已经有人问过这个问题,但这个查询略有不同。也许有人可以帮忙?
我正在向 ios flutter 应用程序发送推送通知。前台处理工作正常。后台/终止模式的工作原理是系统托盘中出现一条通知消息,如果用户单击该消息,则会打开应用程序并发送消息详细信息。
但是,如果我将应用程序放在后台,发送消息,我不会单击系统托盘通知,而是将应用程序带回前台,我可以看到后台处理程序/侦听器没有执行任何操作。因此,如果用户(咳咳,客户端)在后台运行应用程序并忽略系统托盘通知,自行将应用程序带到前台,则不会收到消息数据。
此外,图像的包含也被完全忽略。
xCode 功能包括后台模式(后台获取和远程通知)和推送通知。
我的有效负载如下所示:
"message": {
"token": "my-token-here",
"data": {
"data_value_1": "DATA VALUE 1 test",
},
"notification": {
"title": "NOTIFICATION Title",
"body": "NOTIFICATION Body",
},
"apns":{
"payload":{
"aps":{
"mutable-content": 1,
"content_available": 1,
}
},
"headers": {
"apns-priority": "10",
},
"fcm_options": {
"image": 'https://t4.ftcdn.net/jpg/01/43/42/83/240_F_143428338_gcxw3Jcd0tJpkvvb53pfEztwtU9sxsgT.jpg'
}
}
}
除了 FirebaseMessaging.onMessageOpenedApp.listen 和 FirebaseMessaging.onMessage.listen 处理(两者都有效)之外,我还将其包含在我的 main.dart 文件中,该文件似乎不起作用:
void main() async{
WidgetsBinding widgetsBinding = WidgetsFlufluttertterBinding.ensureInitialized();
...
...
@pragma('vm:entry-point')
Future<void>firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('listener hit (background)...message is received!!!!!!!!!!');
//THE ABOVE NEVER TRIGGERS!;
}
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform );
runApp(MyApp());
}
我已经检查了 Firebase 上的所有文档和类似的帖子,但我无法让它工作......
根据https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages:
..这对我来说,将应用程序从后台放置到前台而不按系统托盘中的通知,将完全忽略该消息。所以问题是,为什么“firebaseMassagingBackgroundHandler”存在?这个只适用于安卓吗?
main.dart 文件
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
runApp(child: MyApp());
}
目标-C:
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
斯威夫特:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
2.AppDelegate.swift
import UIKit
import Flutter
导入 flutter_local_notifications // 添加这一行
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
GeneratedPluginRegistrant.register(with: registry)
}
if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate }
//添加这一行
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
ios通知权限
await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
所有这些都会检查正在运行的 iOS 以正确集成您的代码