1

我已经测试了正确显示的新 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);
4

1 回答 1

0

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

notifMgr.cancel(0); 

单击通知本身应该将其关闭,但由于某种原因,操作似乎没有。您将 0 作为参数传递的原因是因为这是您在调用时提供的 ID:

notifMgr.notify(0, noti);

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

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

于 2012-11-19T18:48:11.313 回答