Home » Android » java – Starting and stopping a notification from broadcast receiver

java – Starting and stopping a notification from broadcast receiver

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to start a status bar notification from a broadcast receiver and then stop it from another broadcast receiver but I’m having issues. I would like to start the notification in the status bar when usb is connected and then when usb is disconnected I would like to stop it I have the two receivers set up and working just struggling with starting and stopping one from a receiver here is the code I have currently

My only error with my code is the myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); line the error just says getSystemService is undefined and it wants to make the method which I guess means that the receiver doesn’t have support for that method like an activity would so what should I do to create and stop a notification from receivers thank you for any help

public class USBConnect extends BroadcastReceiver {

public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence NotificationTicket = "USB Connected!";
      CharSequence NotificationTitle = "USB Connected!";
      CharSequence NotificationContent = "USB is Connected!";

      Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0);
      Intent notificationIntent = new Intent(context, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent);
      notification.flags |= Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID, notification);
      }
}

And then the receiver for when it disconnects this I believe is fine and should work I think my problem is only in the USBConnect class

public class USBDisCon extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}
How to&Answers:

Well i ended up figuring it out myself for future reference for people with my same issue all i needed to do was add context in front of getSystemService here is my code that worked

public class USBConnect extends BroadcastReceiver {

public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence NotificationTicket = "USB Connected!";
      CharSequence NotificationTitle = "USB Connected!";
      CharSequence NotificationContent = "USB is Connected!";

      Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0);
      Intent notificationIntent = new Intent(context, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent);
      notification.flags |= Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID, notification);
      }
}

And then the receiver for when it disconnects this i believe is fine and should work i think my problem is only in the USBConnect class

public class USBDisCon extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}

Answer:

I encountered this problem myself. I think the user forgot to update the code in the answer.
contextneeds to be in TWICE! I misread it the first time!

myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Hope this helps anyone similarly confused!

Answer:

getSystemService undefined most likely means you have not imported android.app.Activity (getSystemService is defined in android.app.Activity).

Answer:

This is the way to use notification from broadcastReceivers:

NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

Schedule alarms from BroadcastReceivers:

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

You have to use the context prefix always!