Reputation: 211
I am trying to display either a MaterialDialog or toast depending on the message received with the push notification. The problem I'm running into however is that the context passed in the WakefulBroadcastReceiver onReceive(Context context, Intent intent) method, is not the the current context of the application. I get a bad window token error when I create a MaterialDialog with it. Does anyone know of a way to get the currently displayed activity's context from the WakefulBroadcastReciever?
Upvotes: 2
Views: 891
Reputation: 3608
To add to Christian answer , use this to send an intent when you get push message
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Then in your activity listen to this event by
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("custom-event-name"));
where mMessageReceiver is a receiver instance where you need to implement onReceive message. See this gist
https://gist.github.com/Antarix/8131277
Upvotes: 1
Reputation: 4641
If your activity is open, your BroadcastReceiver can send a (internal) broadcast message. Your activity has to register for this Broadcast-Type before (in onResume()) and deregister in onPause(). Then your BroadcastReceiver can execute something with the context of your Activity.
Upvotes: 2