It took me a few hours to figure this out. Posting here if anyone else has the same issue.
The idea is that you need to register your activity as a broadcast receiver. The easiest way to do this:
//register your activity onResume() @Override public void onResume() { super.onResume(); context.registerReceiver(mMessageReceiver, new IntentFilter("unique_name")); } //Must unregister onPause() @Override protected void onPause() { super.onPause(); context.unregisterReceiver(mMessageReceiver); } //This is the handler that will manager to process the broadcast intent private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Extract data included in the Intent String message = intent.getStringExtra("message"); //do other stuff here } };
The above code goes into the action you want to listen for events.
Now, how do we send data to this "listener"? Go to the push notification handler (or from which you want to update your activity), and when you receive a notification, call this function:
When you call the above function, your activity should receive it.
Note Your activity must be started / open to receive the broadcast.
Note2 : I switched to a library called 'otto'. This is actually the same, but simpler, "broadcast event" compared to the application. Here's the link http://square.imtqy.com/otto/
Arthur Apr 22 '14 at 6:59 a.m. 2014-04-22 18:59
source share