ios - 如何在 iOS 上使用 Firebase 和 Flutter 获取推送通知?

标签 ios firebase flutter firebase-cloud-messaging apple-push-notifications

我尝试了许多不同的教程,例如这个https://medium.com/flutterpub/enabling-firebase-cloud-messaging-push-notifications-with-flutter-39b08f2ed723

但是我无法在我的 iPhone 上接收任何推送通知。它在 Android 上完美运行。

我使用 firebase_messaging:^4.0.0+4 和 flutter_local_notifications:^0.6.1

问题是尽管使用 getToken() 收到 token ,但在 iOS 中没有调用任何监听器(onMessage、onResume 或 onLaunch)

没有错误信息。我找不到这种行为的原因。如果有人能帮助我,我会很高兴。

谢谢。

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("IM HERE ON MESSAGE");
        print('on message $message');


        String message_as_String = json.encode(message);
        print(message_as_String);
        String title = "test";
        String body = "test";
        String screen = "test";


        var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
            'your channel id', 'your channel name', 'your channel description',
            playSound: false, importance: Importance.Max, priority: Priority.High);
        var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails(presentSound: false);
        var platformChannelSpecifics = new NotificationDetails(
            androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
        await flutterLocalNotificationsPlugin.show(
          0,
          body,
          title,
          platformChannelSpecifics,
          payload: screen,
        );
      },
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');
        String screen = message["screen"];

      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
      },
    );
    if (Platform.isIOS) iOS_Permission();
    _firebaseMessaging.getToken().then((token){
      print(token);
    });
  }

最佳答案

flutter_local_notifications 的文档说明如下:

NOTE: this plugin registers itself as the delegate to handle incoming notifications and actions. This may cause problems if you're using other plugins for push notifications (e.g. firebase_messaging) as they will most likely do the same and it's only possible to register a single delegate.

几个问题:

  • 为什么要同时使用这两个包?
  • 您是否在 Xcode 中的 Target 下的 Background Modes 中启用了Remote notifications
  • 您是否为 iOS 上的通知创建了证书? (APNS:苹果推送通知服务(link)

让我知道你的结果如何 :)

关于ios - 如何在 iOS 上使用 Firebase 和 Flutter 获取推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56027111/

相关文章:

ios - 如何使用 cocoa pod 更新谷歌地图。?

ios - 在 Swift 中从 Realm 加载图像路径的问题

java - android firebase数据库如何从子节点检索所有数据

flutter - 在 flutter 中为不同的 ThemeData 自定义颜色

flutter - Flutter 中带有图像背景的普通 Appbar

ios - 获取持久协调器时核心数据奇怪崩溃

ios - 如何使用 AudioFileStreamSeek 处理流文件?

firebase - Flutter Firebase的多个setData线程仅响应最后一个线程

python - 将 aws 后端连接到 firebase 数据库

flutter - ListView的高度取决于内容的大小