【问题标题】:why do not getting notification in ios with firebase_messaging plugin in flutter?为什么在 Flutter 中使用 firebase_messaging 插件在 ios 中没有收到通知?
【发布时间】:2021-12-21 21:20:35
【问题描述】:

我也在尝试向 android 和 Ios 设备发送通知。 它在 android 设备上工作,但是 ios 令牌有些可疑,没有在 ios 设备上收到通知,我已经为 ios 启用了后台通知。实际上它在几天前就可以工作,但突然停止在 ios 设备上工作。我已经更新了firebase_messaging 插件但仍然没有收到通知,还访问了firebase_messaging issue,this as well

我认为我的 ios 令牌有问题,因为我已经从 pushtry 网站测试并抛出错误说 Please check device token ,但是使用 android 令牌进行测试,没有收到任何错误。所以我可以说 ios 令牌没有正确生成。 我做了我能做的一切,但每次都感到失望。

这是我的测试代码:


class PUSHTest extends StatefulWidget {
 const PUSHTest({Key key}) : super(key: key);

 @override
 _PUSHTestState createState() => _PUSHTestState();
}

class _PUSHTestState extends State<PUSHTest> {
 String token = '';
 var serverKey =
     'AAAAL4uGYoY:.............................';

 @override
 void initState() {
   super.initState();
   getToken();
   showAlertNotification();
 }

 getToken() async {

   String t = await FirebaseMessaging.instance.getToken();
   print(
       "FCM TOKEN: $t");
   setState(() {
     token = t;
   });
 }
 

 showAlertNotification() {
   FirebaseMessaging.instance
       .getInitialMessage()
       .then((RemoteMessage message) {
     if (message != null) {
       print("NOTIFICATIONNNNNNNNNN RESPONSE11${message.data}");
     }
   });

   FirebaseMessaging.onMessage.listen((RemoteMessage message) {
     RemoteNotification notification = message.notification;
     AndroidNotification android = message.notification?.android;
     AppleNotification ios = message.notification?.apple;
     print("ios ios ios:$ios");
     if (notification != null && android != null && !kIsWeb) {
       if (message != null) {
         print("NOTIFICATIONNNNNNNNNN RESPONSE22${message.data}");
       }
     }
   });

   FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
     if (message != null) {
       print("NOTIFICATIONNNNNNNNNN RESPONSE33${message.data}");
     }
   });
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     body: Center(
       child: ElevatedButton(
         onPressed: () => sendNotification(),
         child: Text("SEND NOTIFICATION"),
       ),
     ),
   );
 }


// My FCM Payload To send notification to the device:

 sendNotification() async {
   print("send notification button pressed");
   try {
     http.Response response = await http.post(
       Uri.parse('https://fcm.googleapis.com/fcm/send'),
       headers: <String, String>{
         'Content-Type': 'application/json',
         'Authorization': 'key=$serverKey',
       },
       body: jsonEncode(
         <String, dynamic>{
           'notification': <String, dynamic>{
             'body': 'this is a body',
             'title': 'this is a title',
             "content_available": true 
           },
           'priority': 'high',
           'data': <String, dynamic>{
             'click_action': 'FLUTTER_NOTIFICATION_CLICK',
             'id': '1',
             'status': 'done'
           },
           'to': token,
         },
       ),
     );
     if (response.statusCode == 200) {
       print("SENT NOTIFICATION TO THE DEVICE :$token");
       Fluttertoast.showToast(msg: "SENT NOTIFICATION TO THE DEVICE :$token");
     } else {
       print("error push notification");
       Fluttertoast.showToast(msg: "error push notification");
     }
   } catch (e) {
     print("error push notification");
   }
 }
}

【问题讨论】:

  • 让您拥有通知权限的用户。
  • 您是否在 Firebase 控制台中添加了您的 APN 证书?它也不适用于 iOS 模拟器

标签: flutter dart push-notification firebase-cloud-messaging apple-push-notifications


【解决方案1】:

好吧,我终于发现了我的愚蠢,我到底做了什么。实际上这是两个重大错误。

    其次,我非常愚蠢,我创建了 2 个 firebase 项目,从另一个项目中添加了 android (google-service.json) 和 ios(google-service.plist) 的配置文件。并将server-key 从其中之一添加到后端。 FCM 令牌没有问题。

这就是为什么它只适用于 android。

现在我已经认识到我的愚蠢并删除了重复的 firebase 项目之一,并从原始项目中添加了配置文件并将我的 server-key 更新到我的后端。并使用permission_handler 请求通知许可。就是这样。

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2020-05-25
    • 2019-07-24
    • 2019-12-29
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多