首页 > 解决方案 > Flutter - 单击 FCM 推送通知时,当应用程序是后台时,默认情况下它不会启动应用程序

问题描述

我目前使用 FCM 将通知推送到我的颤振应用程序。

当应用程序处于后台时,当用户点击通知时,我尝试启动应用程序

当我尝试点击通知时,我得到了这个终端日志

    E/FirebaseMessaging(24830): Notification pending intent canceled

我也把这个放在我AndroidManifest.xml官方文档中

    <intent-filter>
        <action android:name="FLUTTER_NOTIFICATION_CLICK" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

这是我的推送通知的有效负载

    { notification: 
       { 
         title: 'title',
         body: 'body',
         badge: '1',
         icon: 'https://miro.medium.com/max/11400/1*lS9ZqdEGZrRiTcL1JUgt9w.jpeg',
         click_action: 'FlUTTER_NOTIFICATION_CLICK',
         sound: 'default' 
       },
      data: { type: '4' } 
    }  

我错过了什么?

标签: flutterfirebase-cloud-messaging

解决方案


首先,您的有效负载应编码为 JSON :

     {
       'notification': {
         'body': 'this is a body',
         'title': 'this is a title'
       },
       'priority': 'high',
       'data': {
         'click_action': 'FLUTTER_NOTIFICATION_CLICK',
         'id': '1',
         'status': 'done'
       },
       'to': await firebaseMessaging.getToken(),
     }

在此之后尝试'click_action': 'FLUTTER_NOTIFICATION_CLICK',从您的有效负载中删除并检查其工作与否


推荐阅读