Flutter应用程序在Android上,当点击通知时无法打开应用程序

3

按照https://pub.dev/packages/firebase_messaging上的说明,我已经实现了 FCM。尽管我设置了click_action: FLUTTER_NOTIFICATION_CLICK,但当我点击通知栏项目时,它并没有打开应用程序。从云函数接收到的数据如下所示。我该如何在通知被点击时至少打开应用程序?

onMessage: {notification: {title: pi, body: text message.}, 
          data: {USER_NAME: pi, click_action: FLUTTER_NOTIFICATION_CLICK, }}

云函数负载
 const payload = {
        notification: {
            title: sender,
            body: content,
            clickAction: 'ChatActivity',
        },
        data: {
            "click_action": 'FLUTTER_NOTIFICATION_CLICK',
            "USER_NAME": sender,
        }
    };

我还创建了通知渠道,并正确配置了AndroidManifest。

private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.app_name);
            String description = getString(R.string.notification_channel_description);
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
1个回答

1
我已将点击操作放入通知的json中,并且对我有效。
notification: {
  title: sender,
  body: content,
  click_action: 'FLUTTER_NOTIFICATION_CLICK'
}

点击操作仅在 onResumeonLaunch 回调中需要,因此请按照 pub.dev 文档中的说明实现它。
 _firebaseMessaging.configure(
       onMessage: (Map<String, dynamic> message) async {
         print("onMessage: $message");
         _showItemDialog(message);
       },
       onBackgroundMessage: myBackgroundMessageHandler,
       onLaunch: (Map<String, dynamic> message) async {
         print("onLaunch: $message");
         _navigateToItemDetail(message);
       },
       onResume: (Map<String, dynamic> message) async {
         print("onResume: $message");
         _navigateToItemDetail(message);
       },
     );

1
我尝试过了,但是不起作用。当我在onMessage中打印payload时,它没有显示clickAction: 'ChatActivity'或click_action: 'FLUTTER_NOTIFICATION_CLICK'。它只显示body和content。 - Patola

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接