-1

My AlertReceiver class is creating notification service. How can i cancel a created notification?

Here is my code:

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

    }
}
Waqas Khan
  • 55
  • 1
  • 10

1 Answers1

2

Use unregisterReceiver(BroadcastReceiver receiver) as a separate function to unregister the Broadcast receiver class from your class.

public void unRegisterAlertreceiver(){
     unregisterReceiver(this);
}
Ryan Godlonton-Shaw
  • 584
  • 1
  • 5
  • 18