firebase - Getting Multiple Notification instead of one in Android -
i using firebase notification in android app. generating notification using below code :
public class firebasenotificationservice extends firebasemessagingservice { private static final string tag = firebasenotificationservice.class.getsimplename(); private string strtitle = "my notification"; private string strmessage = "no description"; @override public void onmessagereceived(remotemessage remotemessage) { if (prefrences.getbooleanvalue(getapplicationcontext(), is_login) && prefrences.getbooleanvalue(getapplicationcontext(), prefrences.is_notification)) { if (remotemessage.getdata().get("title") != null) { strtitle = remotemessage.getdata().get("title"); } if (remotemessage.getdata().get("message") != null) { strmessage = remotemessage.getdata().get("message"); } sendnotification(strtitle, strmessage); } } private void sendnotification(string strtitle, string messagebody) { try { if (!gettopactivity().equals("com.app.activity.notificationactivity")) { intent intent = new intent(this, dashboardactivity.class); intent.putextra("notification", "yes"); intent.addflags(intent.flag_activity_clear_top); pendingintent pendingintent = pendingintent.getactivity(this, 0 /* request code */, intent, pendingintent.flag_one_shot); uri defaultsounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = new notificationcompat.builder(this) .setsmallicon(r.mipmap.app_icon) .setcontenttitle(strtitle) .setcontenttext(messagebody) .setautocancel(true) .setsound(defaultsounduri) .setcontentintent(pendingintent); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(++notification_id /* id of notification */, notificationbuilder.build()); } else { intent intent = new intent(); intent.setaction("load_service"); sendbroadcast(intent); } } catch (exception e) { e.printstacktrace(); } } public string gettopactivity() { activitymanager = (activitymanager) this.getsystemservice(activity_service); list<activitymanager.runningtaskinfo> taskinfo = am.getrunningtasks(1); return taskinfo.get(0).topactivity.getclassname(); } }
the issue multiple notification on status bar generating instead of single, same notification sent backend.
i have taken notification_id static integer in constant file. , incrementing differnt notification.
what might issue ?
do not increase notification id when u receive same notification. need set flag check if notification received same or different notification. when send notification fcm send payload has key-value pair bears notification id. set notification id instead of increasing code. hope helps
Comments
Post a Comment