多个通知可用时,无法取消接受和拒绝操作的通知

时间:2018-04-17 07:06:02

标签: android firebase firebase-cloud-messaging

首先,让我引导您完成整个过程。首先,我实施了一个成功的FCM。我收到了我想要的所有类型的通知。 问题在这里,通知清除(取消)当只有一个通知可用时。但是当时只有一个相同的通知可用,只有一个通知会被取消,这是最后的通知。

我想在接受和拒绝的浏览器上打开网址

我已在下面附上我的代码。

private void sendNotification1(String title, String body, String data) {

    int when = new Random().nextInt();

    String url = "http://google.com";
    String url1 = "http://google.com";
    try {
        JSONObject dataObject = new JSONObject(data);
        url = dataObject.getString("acceptUrl");
        url1 = dataObject.getString("declineUrl");
    } catch (Exception e) {
        e.printStackTrace();
    }

    PendingIntent accceptIntent = NotificationAcceptActivity.getAcceptDismissIntent(this, when, url);
    PendingIntent dismissIntent = NotificationRejectActivity.getAcceptDismissIntent(this, when, url1);


    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
            .setSmallIcon(R.mipmap.ic_notication)
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSound(defaultSoundUri)
            .setPriority(Notification.PRIORITY_HIGH)
            .addAction(R.drawable.ic_close_black_24dp, "Reject", dismissIntent)
            .addAction(R.drawable.ic_check_black_24dp, "Accept", accceptIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(when, notificationBuilder.build());

}

public class NotificationAcceptActivity extends Activity {

public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
public static final String URL = "ACCEPT_URL";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String url = getIntent().getStringExtra(URL);
    Intent dismissIntent = new Intent(Intent.ACTION_VIEW);
    dismissIntent.setData(Uri.parse(url));
    Log.e("NotificationAccept ", "url:- " + url);
    startActivity(dismissIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
    finish();

}

public static PendingIntent getAcceptDismissIntent(Context context, int notificationId, String url) {
    Intent intent = new Intent(context, NotificationAcceptActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(URL, url);
    intent.putExtra(NOTIFICATION_ID, notificationId);
    PendingIntent dismissAcceptIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    return dismissAcceptIntent;
}

}

公共类NotificationRejectActivity扩展了Activity {

public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
public static final String URL = "ACCEPT_URL";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String url = getIntent().getStringExtra(URL);
    Intent dismissIntent = new Intent(Intent.ACTION_VIEW);
    dismissIntent.setData(Uri.parse(url));
    Log.e("NotificationReject ", "url:- " + url);
    startActivity(dismissIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
    finish();

}

public static PendingIntent getAcceptDismissIntent(Context context, int notificationId, String url) {
    Intent intent = new Intent(context, NotificationRejectActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(URL, url);
    intent.putExtra(NOTIFICATION_ID, notificationId);
    PendingIntent dismissAcceptIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    return dismissAcceptIntent;
}

}

1 个答案:

答案 0 :(得分:1)

0应该被所有PendingIntent对象

中的每个唯一编号(请求代码)替换

替换

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

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