如何取消Android中未显示的通知 [英] How can I cancel unshown notifications in android

查看:47
本文介绍了如何取消Android中未显示的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通知的新手,

我为通知编写了这段代码.

I wrote this code for notifications.

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

为此,我设置了一个通知列表.在某些显示和一些不显示.但是我想取消所有未显示的通知.

In that I set one list of notifications. In that some displayed and some not displayed. But I want to cancel all un shown notifications.

我使用了mNotificationManager.cancleAll(),但是只取消了显示的通知. 提前致谢.

I used mNotificationManager.cancleAll() but that is cancel only shown notifications. thanks in advance.

推荐答案

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

I think the problem here is that The Notification is launched immediately after creating it and your not using the AlarmManager to schedule this Notification.

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

Solution Should be to schedule Alarms via an AlarmManager which will trigger the Notification Code above.

这需要3个部分:

注册BroadCastReceiver&触发时生成通知:

Register a BroadCastReceiver & Build a Notification when its triggered:

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,上面的接收方正在监听:

Schedule an Alarm with the AlarmManager to send a BroadCast which the Receiver above is listening for:

        // 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将其从警报队列中删除.

Then when you encounter the situation when you no longer want notifications to show for your app, you tell the AlarmManager to remove them from its queue of Alarms by Rebuilding the pending Intent and using AlarmManager to Cancel it.

       // 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或清单中.

Good Luck, and be sure to register your Receiver in your Activity or Manifest.

这篇关于如何取消Android中未显示的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆