多个通知单击启动相同的活动android

Dev*_*ath 1 android android-pendingintent google-cloud-messaging

我有一个通知设置:

private void sendNotificationForCancellation(String appointmentId, String title, String message) {
        //AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
        Intent intent = new Intent(this, ActCallRequestsDetail.class);
        intent.putExtra("appointment_id", appointmentId);
        intent.putExtra("isAppIdAvailable", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_note_plus)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        Random generator = new Random();
        notificationManager.notify(generator.nextInt(), notificationBuilder.build());
    }
Run Code Online (Sandbox Code Playgroud)

点击通知后,它会打开一个活动

情景1:

  • 当我有一个通知时它工作正常.

. 情景2:

  • 假设从上面的通知设置生成2个通知第一个通知点击,它打开活动现在我在打开的活动中(ActCallRequestsDetail.class)当我点击第二个通知时,没有任何事情发生活动(ActCallRequestsDetail.class)没有刷新

. 如何解决场景-2

Dev*_*ath 6

解决方案只是添加一个新的ID供PendingIntent参考

private void sendNotificationForCancellation(String appointmentId, String title, String message) {
        Random generator = new Random();
       //AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
        Intent intent = new Intent(this, ActCallRequestsDetail.class);
        intent.putExtra("appointment_id", appointmentId);
        intent.putExtra("isAppIdAvailable", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, generator.nextInt(), intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_note_plus)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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


        notificationManager.notify(generator.nextInt(), notificationBuilder.build());
    }
Run Code Online (Sandbox Code Playgroud)