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

时间:2016-10-21 10:24:17

标签: android google-cloud-messaging android-pendingintent

我已设置通知:

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());
    }

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

情境-1:

  • 我收到一条通知时工作正常。

情景-2:

  • 首先假设从上述通知设置生成2个通知 通知点击,它正在打开活动现在我在那里 打开活动(ActCallRequestsDetail.class)当我点击 第二次通知,活动没有任何反应 (ActCallRequestsDetail.class)未刷新

如何解决方案-2

1 个答案:

答案 0 :(得分:6)

解决方案只是为PendingIntent引用

添加新ID
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());
    }