FCM推送通知在iOS设备的Flutter应用程序中不起作用

tcbh2hod  于 9个月前  发布在  iOS
关注(0)|答案(1)|浏览(151)

当Flutter应用在IOS设备上运行时,不会收到FCM推送通知。一旦应用进入后台或最小化,推送通知就会正常工作。
我使用了firebase_messaging和flutter_local_notifications插件。

initialize() async {
   FirebaseMessaging messaging = FirebaseMessaging.instance;
   SharedPreferences sharedPreferences = await 
   SharedPreferences.getInstance();
   NotificationSettings settings = await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  if (settings.authorizationStatus == AuthorizationStatus.authorized) {
    print('User granted permission');
  } else if (settings.authorizationStatus ==
    AuthorizationStatus.provisional) {
      print('User granted provisional permission');
  } else {
    print('User declined or has not accepted permission');
  }

  messaging.getToken().then((token) {
    print("FCM TOKEN:- $token");
    sharedPreferences.setString("fcmToken", token.toString());
  });

  FirebaseMessaging.onMessage.listen((RemoteMessage event) {
    print("message received");
    print(event.notification!.body);
    flutterLocalNotificationsPlugin.show(
      1,
      event.notification!.title.toString(),
      event.notification!.body.toString(),
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          color: Colors.amber,
          playSound: true,
          icon: '@mipmap/ic_launcher',
        ),
      ));
  });
  FirebaseMessaging.onMessageOpenedApp.listen((message) {
    print('Message clicked!');
  });
}

字符串
有人能帮帮我吗?

k4aesqcs

k4aesqcs1#

当你在onMessage中监听通知时,你只添加了android规范。请同时添加IOS规范。

var iOSPlatformChannelSpecifics = const DarwinNotificationDetails(
presentBadge: true,
presentAlert: true,
presentSound: true,
interruptionLevel: InterruptionLevel.active);

字符串

相关问题