簡體   English   中英

如何停止將新的推送通知替換為Firebase中的舊推送通知?

[英]How to stop new push notification being replaced by the old one in Firebase?

當我觸發推送通知時,舊的將被新的替換。 但我希望它應該添加到該舊通知中。

我的MyFirebaseMessaging類是:

public class MyFirebaseMessaging  extends FirebaseMessagingService {
    String title="";
    String message,type,click_action;
    int no_of_messages;

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

        if(remoteMessage.getData().size()>0) {
            message = remoteMessage.getData().get("body");
            title = remoteMessage.getData().get("title");
            click_action = remoteMessage.getData().get("click_action");
            no_of_messages++;
            ShowNotification(message);
        }
        else
            Log.i("remoteMessage",message);
    }

    private void ShowNotification(String message) {
        Intent intent = new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("loading_flag","load");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
        // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //   notificationBuilder.setSmallIcon(R.drawable.vadodaramunicipalcorporation);
        //  notificationBuilder.setColor(getResources().getColor(R.color.backgroundColor));
        //  } else {
        //     setSmallIcon(R.drawable.vadodaramunicipalcorporation)
        //  }
        notificationBuilder.setSmallIcon(R.mipmap.vadodaramunicipalcorporation)
            .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.vadodaramunicipalcorporation))
            .setContentTitle("You have new "+ title)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setTicker("Hearty365")
            .setContentInfo("Info")
            .setContentText(message+" new Alerts")
            .setNumber(no_of_messages)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setStyle(new NotificationCompat.BigTextStyle())
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX);
        notificationManager.notify(0, notificationBuilder.build());
    }
}


{
    "to":"token-key",
    "data" : {
         "body" : "4",
          "title" : "Alert",
          "click_action" : "Activity.DATA_CLICK"
    }
}

新的推送通知被替換為我的應用程序中Firebase推送通知中的舊推送通知,我將如何停止

您需要在notificationManager.notify()不同的notificationID

樣本代碼

Date myDate = new Date();
int myNotificationId = Integer.parseInt(new SimpleDateFormat("ddhhmmss",  Locale.US).format(myDate));
notificationManager.notify(myNotificationId,notificationBuilder.build());

你必須像下面這樣

private final static AtomicInteger c = new AtomicInteger(0);

    public static int getID() {
        return c.incrementAndGet();
    }

您的清單行應替換為以下內容

notificationManager.notify(getID(), notificationBuilder.build());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM