简体   繁体   中英

FCM Multiple push notifications not working properly when app in background state

I am using fcm for push notifications, I am handling only data payload. Multiple Notifications are working fine when app is in foreground, here I am using id value from server in click_action to move the notification to related post when we tap on it.But, when app is in background/closed state whenever my getting multiple notifications, I will tap on one notification it will goes to related post but remaining all are not redirecting to related post, just cleared from notification. I didn't find out why this problem happening.Here is my code

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());


        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
        }

private void sendNotification(String title, String message, String id, String image) {
    Log.d("notificationdetails",title+",,,"+message+",,"+id);
    Intent  intent = null;



int postid = Integer.valueOf(id);
        if(id.equals("")|| id==null) {
            intent = new Intent(this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }

else{

        Constants.PushId = postid;
        intent = new Intent(this, DetailActivity.class);
        Log.d("mypostid",postid+"");
        intent.putExtra("id", postid);
        intent.putExtra("backpage","main");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, postid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_primepost)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random random = new Random();
    int num = random.nextInt(99999-1000)+1000;
    notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
}

}

Manifest.xml

 <activity android:name="com.primepostnews.app.Activities.DetailActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="Detail" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed.When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages. The Firebase console always sends notification messages.

 if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);

        } else if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getBody(),remoteMessage.getData().get("click_action"),image);
        }

You Should use a Notification type message to work in All the case. Data Type not work when the App is Killed / In background. Because for the Data Type notification The OnMessageRecieved will called and you have to manually create status bar notification using the NotificationBuilder.

So always better to use Notification Type input from your server like below

"notification": {
            "title": "Firebase notification",
            "message": "I am firebase notification. you can customise me. enjoy",
            "click_action": "OPEN_ACTIVITY",
            "sound":"default",

        }

This will work in all the case, And you don't want to worry about handling it on your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM