0

how to fire notification from BroadcastReceiver (can't use most methods and can't use "this")? I need it to open a activity with info from the DB I already did it but now must of the methods dosen't work and I cant use "this"

ofeking109
  • 71
  • 1
  • 11

1 Answers1

1

In the onReceive method you get a Context object. So use it to get the NotificationManager and fire your notification.

public void onReceive(Context ctx, Intent intent) {
    NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    //Create the notification here.
    nm.notify(NOTIFICATION_ID, notification);
}

An Activity and a Service are derived from Context. That's why, in many (or all) of the instance methods of a context, you can use this. If that's your case, then you can use the Context you receive in onReceive.

Jong
  • 9,045
  • 3
  • 34
  • 66
  • For my use case, what if there are hundreds of pendingIntents for triggering Alarms, that need to be reset after the BroadcastReceiver receives a BOOT_COMPLETED action? Since there are many alarms how would I reset these alarms in the background? – AJW Oct 25 '21 at 00:36