0

Если приходит например 3 пуша, то по нажатию на любой открывается инфа с последнем пушем. Его параметры в intent уходят.

Как сделать чтобы с каждого пуша свои открывались данные?

public class MessagingService extends FirebaseMessagingService {

private int obj_type = 0;
private long id = 0;
private String title = "title";
private String text = "";
private static int count = 0;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("PUSH", remoteMessage.getData().toString());

    //SharedPreferences sharedPreferences = getSharedPreferences("push", MODE_PRIVATE);
    //SharedPreferences.Editor editor = sharedPreferences.edit();

    if (remoteMessage.getData() != null) {
        try {
            obj_type = Integer.parseInt(remoteMessage.getData().get("obj_type"));
            //editor.putInt("obj_type", obj_type);
        } catch (Exception e) {
        }

        try {
            id = Long.parseLong(remoteMessage.getData().get("id"));
            //editor.putLong("id", id);
        } catch (Exception e) {
        }

        try {
            text = remoteMessage.getData().get("text");
        } catch (Exception e) {
        }

        try {
            title = remoteMessage.getData().get("title");
        } catch (Exception e) {
        }
        //editor.apply();

        sendNotificationPushNewMessages();
    }
}


private void sendNotificationPushNewMessages() {
    Intent intent;

    if (MainActivity.OPEN_APP) {
        intent = new Intent(this, MainActivity.class);
    } else {
        intent = new Intent(this, LoadingActivity.class);
    }
    intent.putExtra("id_push", id);
    intent.putExtra("obj_type_push", obj_type);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
            .setContentTitle(title)
            .setContentText(text)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setContentInfo("some_info");

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(count++, b.build());
}
}

1 ответ 1

3

У вас PendingIntent имеет идентификатор 0 (второй параметр), соответственно при создании нового интента с таким же идентификатором он просто обновляет старый, к тому же указан флаг PendingIntent.FLAG_UPDATE_CURRENT, который говорит обновлять данные этому интенту. Нужно на каждый новый PendingIntent ставить новый id PendingIntent contentIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.