Patola
Patola

Reputation: 673

Flutter app on android does not open app when notification is clicked

Following instructions on https://pub.dev/packages/firebase_messaging, I have implemented FCM. Although I have set click_action: FLUTTER_NOTIFICATION_CLICK, when I click on the notification tray item, it does not open the app. The data received by the flutter app from the cloud function is as follows. How do I at least open the app upon notification click?

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

Cloud function payload

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

I have also created the notification channels and AndroidManifest has been correctly configured.

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

Upvotes: 3

Views: 3345

Answers (2)

Suhad Bin Zubair
Suhad Bin Zubair

Reputation: 804

I am not sure its complete working, But for me its worked,

Add android:launchMode="singleTop" inside the in AndroiManifest

Upvotes: 0

Leonard Arnold
Leonard Arnold

Reputation: 664

I have put the click action to the notification json and it works for me.

notification: {
  title: sender,
  body: content,
  click_action: 'FLUTTER_NOTIFICATION_CLICK'
}

The click action is only required for onResume and onLaunch callbacks, so be sure to implement it as stated in the pub.dev doc:

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

Upvotes: 1

Related Questions