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>