简体   繁体   中英

Firebase multiple push notification not working

If i am sending 3 push data notification to my app using firebase, the one which I clicked first is only opening and all the data is shown whlie rest 2 when clicked never opens my app so. Below is my given code for receiving the data message, please have a look.

 private void sendNotification(String message, Bitmap image, String objIdOS, String objIdVenInv, String typeVenInv, String cls, String checkTest, String colName, String title, String position) {
    Intent intent = new Intent(this, UserLogin.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("objIdOS",objIdOS);
    intent.putExtra("objIdVenInv",objIdVenInv);
    intent.putExtra("typeVenInv",typeVenInv);
    intent.putExtra("cls",cls);
    intent.putExtra("checkTest",checkTest);
    intent.putExtra("colName",colName);
    intent.putExtra("pos",position);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setAutoCancel(true)
            .setSound( Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.clean))
            .setOnlyAlertOnce(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setColor(Color.parseColor("#1ABC9C"))
            .setSmallIcon(R.drawable.small_icon);
    if (image!=null){
        notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(message));  /*Notification with Image*/
    }

    /*if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.splash_icon3));
    } else {
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.splash_icon3));
    }*/

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(notificationId, notificationBuilder.build());
}

add this

    (int)SystemClock.currentThreadTimeMillis()

    PendingIntent.getActivity(this, (int)SystemClock.currentThreadTimeMillis() /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);


notificationManager.notify((int)SystemClock.currentThreadTimeMillis(), notificationBuilder.build());

Change these from

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

  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);

To

int requestCode = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, 
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

it will generate random request code, so that multi notification can be open when tapped..

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