Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open particular screen on clicking on push notification for flutter

I am trying to achieve open specific screen on clicking push notification and my payload looks like this:

 var payload = {         notification: {             title: notificationTitle,             body: notificationMessage,             click_action:"/screena",sound:"default",         }     }; 

I am getting notification but I am unable to catch notification click event in flutter how to catch it. I am using flutter messaging

https://github.com/flutter/plugins/tree/master/packages/firebase_messaging

and my firebase push message service code looks like this

 pushMessagingService() async{ messagingreference.configure( onMessage: (Map<String, dynamic> message) {    print("I am here in on message");   print(message); }, onLaunch: (Map<String, dynamic> message) {   print("I am here onLaunch");   print(message); }, onResume: (Map<String, dynamic> message) {   print("I am hereonResume");   print(message); }, );   messagingreference.requestNotificationPermissions(   const IosNotificationSettings(sound: true, badge: true, alert: true));  messagingreference.onIosSettingsRegistered   .listen((IosNotificationSettings settings) { print("Settings registered: $settings");  });  messagingreference.getToken().then((String token) async {   print(token);  });  } 

here I can get the message as @xqwzts said in on message when my app is in the foreground but my question is how to catch click event from push notification raised in the system tray and navigate to the required screen.

like image 299
siva kumar Avatar asked Jan 23 '18 14:01

siva kumar


People also ask

How do I open my screen when I click Notifications on flutter?

Step-1: pass one key-value pair in the firebase notification as click_action: FLUTTER_CLICK_ACTION. Step-2: Using step 1 you'll receive the onTap callback of notification inside onResume or onLaunch method. Step-3: Perform the following scenario for navigating to a specific screen on click of notification.

How do I trigger Push Notifications on flutter?

In the main app target, select Signing & Capabilities > All > + Capability and then search "push." Double-click on Push Notifications to enable it. Next, enable Background Modes and check Remote Notifications. Now you are ready to send notifications from the OneSignal dashboard!

How do you send a push notification to a specific user flutter?

To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).

How do I schedule notifications on flutter?

After the Initialization, the notification can be scheduled using zonedschedule() method. In that, we should give notification details like Title, description, etc. Android and iOS get their own notification details like android has channels, so we should give channel id, channel name, importance, priority, etc.


1 Answers

A few things here:

1- click_action has to be set to "FLUTTER_NOTIFICATION_CLICK"

2- click_action has to be set in the data section of a payload

DATA='{   "notification": {     "body": "this is a body",     "title": "this is a title",   },   "data": {     "click_action": "FLUTTER_NOTIFICATION_CLICK",     "sound": "default",      "status": "done",     "screen": "screenA",   },   "to": "<FCM TOKEN>" }' 

This should allow you to receive the message in the onMessage handler in your flutter app.

From there you can call Navigator.of(context).pushNamed(message['screen']).

If you don't have a BuildContext at that point, you can register a GlobalKey as the navigatorKey property of your MaterialApp, and use it to access your Navigator globally, via GlobalKey.currentState

like image 157
xqwzts Avatar answered Oct 02 '22 05:10

xqwzts