问题描述
在我的fcm云功能中,它可以向其他播放器发送通知,但是当我添加apns图层时,它无法发送。删除apns层后,通知会显示在android上,但对于iOS,通知是在前端接收的,但无法触发onMessage函数来显示警报对话框。
var payload = {
data: {
click_action: "FlutteR_NOTIFICATION_CLICK",notificationType: "friendsRequest",fromUsername: fromUsername,fromProfilePic: fromProfilePic,fromColorIndex: fromColorIndex,type: type
},};
此有效负载返回服务器响应“崩溃”
解决方法
要为iOS启用通知,您需要先为iOS设置FCM: 如您所说,您已经生成了APN。只需确认您是否已将apn与firebase项目链接。之后,在Xcode中打开您的项目ios模块,并在AppDelegate中进行以下更改
if #available(iOS 10.0,*) {
UNUserNotificationCenter.current().delegate = self as?
UNUserNotificationCenterDelegate
}
在颤动侧进行更改(我在启动屏幕中使用此代码):
初始化
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
initState()中的代码
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher'); //replace with your app icon file
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid,initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification: onSelectNotification);
FirebaseNotifications().setUpFirebase(_firebaseMessaging);
_firebaseMessaging.configure(
onMessage: (Map<String,dynamic> message) async {
showNotification(
message['notification']['title'],message['notification']['body']);
print("onMessage: $message");
},onLaunch: (Map<String,dynamic> message) async {
print("onLaunch: $message");
},onResume: (Map<String,dynamic> message) async {
print("onResume: $message");
},);
通知方法:
void showNotification(String title,String body) async {
await _demoNotification(title,body);
}
Future<void> _demoNotification(String title,String body) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_ID','channel name','channel description',importance: Importance.Max,playSound: true,sound: 'sound',showProgress: true,icon: 'notification_icon',color: AppTheme.secondaryaccent,priority: Priority.High,ticker: 'test ticker');
var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics,iOSChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0,title,body,platformChannelSpecifics,payload: 'test');
}