FCM Push Notification Android receiving 2 notifications in the background

FCM Push Notification Android receiving 2 notifications in the background



I'm having a problem using the FCM Push Notification Messaging Service, as I've overridden the handleIntent() method to receive the notification when the app is in the foreground. I am also using the onMessageReceived() method.


handleIntent()


onMessageReceived()



But when the app is in the background, I will receive 2 notifications, which one of them only opens up the app and runs the MainActivity while the other is opening up the app how I want it to.


MainActivity



FYI: The notification I receive when I am in the foreground is exactly how I want it to open.



This is the code I've written below :


public class MyFirebaseMessagingService extends FirebaseMessagingService

private final String NOTIFICATION_TYPE = "type";
private final String NOTIFICATION_ID = "id";
private final String NOTIFICATION_TYPE_PRODUCT_DETAIL = "productdetail";

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);

String title = remoteMessage.getNotification().getTitle();R
String body = remoteMessage.getNotification().getBody();

String token = remoteMessage.getFrom();
Log.d("FireBase TAG: ", token);

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
Log.d("FireBaseMessageService","FireBase Data payload : " + remoteMessage.getData());


// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());







@Override
public void handleIntent(Intent intent)
super.handleIntent(intent);

String type = intent.getExtras().getString(NOTIFICATION_TYPE, "");

int id = 0;
try
id = Integer.valueOf(intent.getExtras().getString(NOTIFICATION_ID, ""));
catch (NumberFormatException e)
e.printStackTrace();

//Intents
Intent mainIntent = MainActivity.newIntent(this);
Intent editProfileIntent = EditProfileActivity.newIntent(this);
Intent settingsIntent = SettingsActivity.newIntent(this);
Intent productIntent = ProductActivity.newNotificationIntent(this, id, false, true);


if (UserManager.getSingleton().isUserLoggedIn(getApplicationContext()))

PendingIntent pendingIntent;

if (type.equalsIgnoreCase(NOTIFICATION_TYPE_PRODUCT_DETAIL))
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(mainIntent);
stackBuilder.addNextIntent(productIntent);

editProfileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

else
pendingIntent = PendingIntent.getActivity(this, 0, productIntent,
PendingIntent.FLAG_ONE_SHOT);


NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(intent.getExtras().getString("gcm.notification.title"))
.setContentText(intent.getExtras().getString("gcm.notification.body"))
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());






I have deleted the NotificationCompat.Builder from the onMessageReceived() method.
But I am still receiving two notifications in the background.


NotificationCompat.Builder


onMessageReceived()



App Gradle :


compile 'com.google.firebase:firebase-core:11.4.2' //Firebase
compile 'com.google.firebase:firebase-messaging:11.4.2' //Firebase Cloud Messaging



I've tried searching for a solution online but unluckily there isn't a solution pointing to Android.




5 Answers
5



You are handling your Notification stuff into handleIntent(Intent intent). You should probably remove super.handleIntent(intent); to prevent the Firebase system to handle notification while the app is in background.


handleIntent(Intent intent)


super.handleIntent(intent);



Solution: remove super.handleIntent(intent);


super.handleIntent(intent);





Hi man, thanks, you saved my day!~~ Anyway, do you know why when I tapped on the notification, it doesn't get removed from the tray?
– WiL BaJaMas
Jan 4 at 7:49





Use builder.setAutoCancel(true), while creating Notification.
– Paresh P.
Jan 4 at 8:09


builder.setAutoCancel(true)



Just make a sendnotification() method and set whatever parameters you want to pass like body i.e sendnotification(String body). Use pending intent to start you activity and when you click on notification your app parse the data to the launcher activity which is defined in manifest, so once you have data in your launcher activity you can send data to other activity using intent.



I think the .setContentText("") is getting called more than 1 times and are you getting same notification two times?



The notification which works perfectly is generated by your code but when your application is not in foreground android system will generate the notification for you. In this case when you don't have the control to send data in your intent that you were sending to open your desired Activity.
In this case, you have to do some modification on your servers payload. You have to add click_action in your payload, this is how android system will identify the destination activity.



Payload Example:



"notification":


"title":"Notification title",

"body":"Notification body",

"click_action":"<Your_destination_activity>",

, "data":

"param1":"value1",

"param2":"value2"

,

"priority":"high",





Reference:
https://firebase.google.com/docs/cloud-messaging/http-server-ref



yes,
When you app in background you will receive the push at system tray so system tray will create push with notification title and message.
and when you click on the push your initial launcher activity (which mentioned as launcher in manifest) will open.



you can get your notification data at you launcher activity (bundle).


private void handlePush()
Intent intent = null;
if (bundle.getString("push_type") != null && bundle.getString("push_type").length() > 0)

switch (bundle.getString("push_type"))
case PUSH_TYPE_FOLLOW_USER: Intent.FLAG_ACTIVITY_NEW_TASK);
break;




if (intent != null)
startActivity(intent);
finish();




and you need to check activty have data or not


if (bundle != null)
handlePush();
else //your next activity



FYI : https://firebase.google.com/docs/cloud-messaging/android/receive



or



you can get payload object instead of data object inside notification , if you have payload object in your notification object, push all time received at your onMessageReceived().



for people still having this issue:



Here is a hack to prevent this behavior. I've looked all over and there seems to be minimal info about this, but if you save the actual message being sent in shared preferences and then do a check against that value in onRecieve, you can easily prevent this. The only downside is that your user can't send the exact same message two times in a row in the form of a notification (but that would be annoying anyway). example:


@Override
public void onMessageReceived(RemoteMessage remoteMessage)
SharedPreferences Settings = getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = Settings.edit();

message = remoteMessage.getNotification().getBody();
from = remoteMessage.getFrom(); //this comes through as the topic, oddly...

if(from.equals("/topics/"+userID+deviceID+"all"))

if(!message.equals(Settings.getString("messageall",null))) //this filters any dupe messages

utils.postNotification(title, message, context, extra, "messages");//create notification
editor.putString("messageall", message);//always update to the last message
editor.commit();





Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)