简体   繁体   English

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

[英]How to get Push-Notification on iOS working with Firebase and Flutter?

I tried many different tutorials, followed eg this one https://medium.com/flutterpub/enabling-firebase-cloud-messaging-push-notifications-with-flutter-39b08f2ed723我尝试了许多不同的教程,例如这个https://medium.com/flutterpub/enabling-firebase-cloud-messaging-push-notifications-with-flutter-39b08f2ed723

But I'm not able to receive any Push-Notifications on my iPhone.但是我无法在我的 iPhone 上接收任何推送通知。 It works perfectly on Android.它在Android上完美运行。

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

The problem is that none of the listeners (onMessage, onResume or onLaunch) is getting called in iOS although a token is received with getToken()问题是,尽管使用 getToken() 接收到令牌,但在 iOS 中没有调用任何侦听器(onMessage、onResume 或 onLaunch)

There is no error message.没有错误信息。 I can't find the reason for this behavior.我找不到这种行为的原因。 I would be glad if someone could help me out.如果有人可以帮助我,我会很高兴。

Thanks.谢谢。

_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);
    });
  }

The documentation for flutter_local_notifications states the following: 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 (eg firebase_messaging) as they will most likely do the same and it's only possible to register a single delegate.如果您使用其他插件进行推送通知(例如 firebase_messaging),这可能会导致问题,因为它们很可能会执行相同的操作,并且只能注册一个委托。

A few questions:几个问题:

  • Why do you want to utilise both packages?为什么要同时使用这两个软件包?
  • Have you enabled Remote notifications in Background Modes under your Target in Xcode?您是否在 Xcode 的Target下的Background Modes启用了Remote notifications
  • Have you created a certificate for notifications on iOS?您是否为 iOS 上的通知创建了证书? (APNS: Apple Push Notification Service ( link ) (APNS:Apple 推送通知服务( 链接

Let me know how this turns out for you :)让我知道结果如何:)

I know this is quite late, but I had the same issue.我知道这已经很晚了,但我遇到了同样的问题。

My problem was that I didn't download the 'GoogleService-Info.plist' again after I added the Key to Firebase.我的问题是在将密钥添加到 Firebase 后,我没有再次下载“GoogleService-Info.plist”。

After downloading it again and adding it to the Xcode project, push notifications worked like a charm :)再次下载并将其添加到 Xcode 项目后,推送通知就像一个魅力:)

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM