如何在android中取消未显示的通知

时间:2015-05-06 11:18:33

标签: android notifications android-notifications android-pendingintent

您好我是通知新手,

我为通知编写了此代码。

PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus,
    myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.noti)
    .setContentTitle("LMS notification")
    .setContentIntent(pendingIntent)
    .setContentText(intent.getStringExtra("messagenotify"))
    .setAutoCancel(true);

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());

在那里我设置了一个通知列表。在一些显示和一些不显示。但我想取消所有未显示的通知。

我使用mNotificationManager.cancleAll()但是取消仅显示通知。 提前致谢。

3 个答案:

答案 0 :(得分:1)

我认为这里的问题是,在创建通知后立即启动通知,而您没有使用AlarmManager来安排此通知。

解决方案应该通过AlarmManager安排警报,这将触发上面的通知代码。

这需要3个部分:

注册BroadCastReceiver&触发时构建通知:

public class UtilityReceiver  extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   // .. Here is where you really want to build the notification and notify
   // This will be triggered by the AlarmManager from the Code below
   PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
               .setSmallIcon(R.drawable.noti)
               .setContentTitle("LMS notification")
               .setContentIntent(pendingIntent)
               .setContentText(intent.getStringExtra("messagenotify"))
               .setAutoCancel(true);

     i = (int) System.currentTimeMillis();
     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
     mNotificationManager.notify(i, mBuilder.build());

}

使用AlarmManager安排闹钟以发送上述接收者正在收听的BroadCast:

        // Create an Intent to Launch
        Intent notificationIntent = new Intent(context, UtilityReceiver.class);

        // Use a Pending Intent to Launch the Alarm
        PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
                            PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, timeForAlarmInMillis, notificationPendingIntent);

然后当您遇到不再需要为应用程序显示通知的情况时,通过重建待处理的Intent并使用AlarmManager取消它,告诉AlarmManager将其从警报队列中删除。

       // Create an Intent to Launch
        Intent notificationIntent = new Intent(context, UtilityReceiver.class);

        // Use a Pending Intent to Launch the Alarm
        PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
                            PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // *********Important this will cancel all the Matching PendingIntents
        alarmManager.cancel(notificationPendingIntent);

祝你好运,并确保在Receiver或清单中注册Activity

答案 1 :(得分:0)

我认为你正在搜索类似的内容,

How to dismiss notification after action has been clicked

只需检查该帖子上的答案即可。使用id取消该特定通知

我认为代码中的id部分就是这个,

i = (int) System.currentTimeMillis();

答案 2 :(得分:0)

NotificationManager维护所有通知mNotificationList的列表,并在此列表上执行cancelcancelAll次操作

我尝试使用notify()enqueueNotificationWithTag()cancelAllNotification()中的 NotificationManager.java NotificationManagerService.java 的Google源代码进行搜索但是没有实现取消之前的未显示的通知

根据您的评论,如果您的用户被删除并且他的通知到了,则用户已被删除,因此通知没有意义

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());
// i should be Id for this notification, i think you want to pass 
// time in mili second here