如何取消BroadcastReceiver的通知
How to cancel notification of BroadcastReceiver
我的 AlertReceiver class 正在创建通知服务。如何取消已创建的通知?
这是我的代码:
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"Title","This is message body", "Alert");
}
public void createNotification(Context context,String msg,String msgText, String msgAlert){
PendingIntent notificIntent=PendingIntent.getActivity(context, 0,
new Intent(context,MainActivity.class),0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.my_image)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager=
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
}
}
使用 unregisterReceiver(BroadcastReceiver receiver) 作为一个单独的函数从您的 class.
注销广播接收器 class
public void unRegisterAlertreceiver(){
unregisterReceiver(this);
}
我的 AlertReceiver class 正在创建通知服务。如何取消已创建的通知?
这是我的代码:
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"Title","This is message body", "Alert");
}
public void createNotification(Context context,String msg,String msgText, String msgAlert){
PendingIntent notificIntent=PendingIntent.getActivity(context, 0,
new Intent(context,MainActivity.class),0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.my_image)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager=
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
}
}
使用 unregisterReceiver(BroadcastReceiver receiver) 作为一个单独的函数从您的 class.
注销广播接收器 classpublic void unRegisterAlertreceiver(){
unregisterReceiver(this);
}