android - Can't start Application to particular activity on Notification click from FCM -


i'm experiencing problem: if click on notification , app closed, homeactivity opened instead of requested one.

i'm raising notification this:

intent intent = new intent(context, articleactivity_.class); intent.putextra(articleactivity.key_article_id, articleid); intent.setflags(intent.flag_activity_new_task); pendingintent pendingintent = pendingintent.getactivity(context, 0 , intent,             pendingintent.flag_update_current);   uri defaultsounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = new notificationcompat.builder(context)             .setsmallicon(r.drawable.ic_launcher)             .setcontenttitle(notification.gettitle())             .setcontenttext(notification.getbody())             .setautocancel(true)             .setsound(defaultsounduri)             .setcontentintent(pendingintent);  notification n = notificationbuilder.build(); n.flags |= notification.flag_auto_cancel;  notificationmanager notificationmanager =             (notificationmanager) context.getsystemservice(context.notification_service);  notificationmanager.notify(0 , n); 

and works fine if application opened. if close it, after clicking on notification, homeactivity opened instead of 1 configured.

you having problem because how fcm works. there 2 types of push notifications. first type "notification" , second type "data". both can visualize json payload:

"notification": {     //key values }  "data": {     //your custom key values } 

there third type combination of both.

the "notification" type create default visual notification default behavior when app not in foreground. , execute whatever write inside onmessagereceived when app open. second type "data" code inside onmessagereceived.

from firebase web console, can send "notification" type or combined type. sending "data" need send firebase functions or server (that complies fcm requisites).

there trick can goal. combined payload, can send firebase web console, contains "notification" , "data". "data" key-values available after user click notification. information available data intent opening default launcher activity, there can change activity.

in launcher activity, add following:

string notification = getintent().getstringextra("your-key"); if (notification != null) {     startactivity(new intent(this, youractivity.class)); } 

this way can validate activity being opened because default notification clicked, , string because payload in push fcm must rule string. in other cases, if user opened app, string null, , don't redirect user.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -