addAction时通知不会自动取消

时间:2012-08-09 11:53:45

标签: android notifications android-4.2-jelly-bean

我测试了正确显示的新BigTextStyle通知。但是,当我向通知添加操作时,它永远不会取消,并且在单击操作时下拉通知区域永远不会关闭。我做错了什么?

    Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add);
    Bitmap b = ((BitmapDrawable)d).getBitmap();
    Notification noti = null;

    Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);

    String s = "Name: John \n"
            + "Surname: Doe\n"
            + "Vehicle: Honda Jazz TA-1234\n"
            + "Note: Good drifter";

    Builder builder = new Notification.Builder(MainActivity.this)
            .setContentTitle("New challenger arrived!")
            .setContentText(s)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(b)
            .setTicker("New challenger arrived!")
            .setPriority(Notification.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setNumber(5)
            .addAction(
                    android.R.drawable.ic_menu_add,
                    "Accept",
                    PendingIntent.getActivity(getApplicationContext(), 0,
                            notificationIntent, 0, null))
            .addAction(
                    android.R.drawable.ic_menu_delete,
                    "Refuse",
                    PendingIntent.getActivity(getApplicationContext(), 0,
                            notificationIntent, 0, null))
            .addAction(
                    android.R.drawable.ic_dialog_map,
                    "Map",
                    PendingIntent.getActivity(getApplicationContext(), 0,
                            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null));

    noti = new Notification.BigTextStyle(builder)
                        .setBigContentTitle("New challenger arrived!")
                        .bigText(s)
                        .setSummaryText("You will never win against me").build();

    NotificationManager nm = (NotificationManager) MainActivity.this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    nm.notify(0, noti);

1 个答案:

答案 0 :(得分:0)

我认为如果你采取行动,你必须手动取消通知。这可以通过调用

来完成
notifMgr.cancel(0); 

单击通知本身应该忽略它,但由于某种原因,似乎没有动作。你传递0作为参数的原因是因为你打电话给你的ID是:

notifMgr.notify(0, noti);

这是我更新的答案,但是,我之前已经回答过这个问题:

看起来您正在尝试启动您当前显示的相同活动(来自您的PendingIntent)(我假设这是一个1活动应用,MainActivity包含上面的代码)。创建另一个活动,构建您的PendingIntent,以便启动该活动,然后它应该按照您的意愿执行。或者,运行您的活动,显示通知,导航到主屏幕或其他应用,然后根据您的活动操作,您所需的行为应该显示。