【问题标题】:Flutter app on android does not open app when notification is clicked单击通知时,Android上的Flutter应用程序不会打开应用程序
【发布时间】:2020-04-04 15:00:41
【问题描述】:

按照https://pub.dev/packages/firebase_messaging 上的说明,我已经实现了 FCM。虽然我设置了click_action: FLUTTER_NOTIFICATION_CLICK,但是当我点击通知托盘项时,它并没有打开应用程序。 Flutter App从云端函数接收到的数据如下。我如何至少在点击通知时打开应用程序?

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

【问题讨论】:

    标签: android firebase flutter firebase-cloud-messaging firebase-notifications


    【解决方案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);
           },
         );
    

    【讨论】:

    • 我试过了,但是不行。当我在 onMessage 上打印有效负载时,它不显示 clickAction: 'ChatActivity' 或 click_action: 'FLUTTER_NOTIFICATION_CLICK'。它只显示正文和内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多