Refreshing activity upon receipt of a gcm push notification

How to update activity when receiving gcm push notifications if my application is open . I have an activity that contains a listview populated with data from the server. I want to update my activity ( by adding another item to listview ) if I get a gcm push notification (which also contains some data).

  • One option is to add a timer that periodically executes server requests and updates the list adapter data, but I do not want them because it will require a lot of resources.
  • Do I need to add a broadcast receiver that will start when gcm push is received, which then requests new server data and updates my activity interface?

Dear commentators, please read the question carefully, I only need to update the list (if the application is open and this particular activity is open) otherwise there is no need for the same .

+48
android android-activity android-listview google-cloud-messaging
Mar 07 '14 at 1:56
source share
7 answers

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:

 // This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with static void updateMyActivity(Context context, String message) { Intent intent = new Intent("unique_name"); //put whatever data you want to send, if any intent.putExtra("message", message); //send broadcast context.sendBroadcast(intent); } 

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/

+134
Apr 22 '14 at
source share

I assume your GCMBroadcastReceiver has its own .java file in it?

As for updating activity, I would also like to know the answer to this question.

But in order to find out if any particular activity is active, the value on the screen simply adds boolean (call it something like β€œactive”) and set the value to true in your onResume() event, and then false in the onPause() :

 protected void onResume() { super.onResume(); active = true;; } protected void onPause() { super.onPause(); active = false; } 

Your active variable will be boolean, which is global or static . Thus, you know that a certain activity is in the "forward" mode.

Hope this helps.

+3
Mar 12
source share

The response to the reception is really correct for "Updating activity when receiving gcm push notification" (I also supported it). But if you want to update the ListView that is displayed, you do not need a broadcast receiver.

The GCM listener service can update the database using the ContentProvider, rather than embed a direct SQL query.

Then you can rely on the notifyChange method on the ContentResolver to do the trick.

Notify registered observers that the row has been updated. To register, call registerContentObserver (). By default, CursorAdapter objects will receive this notification. If syncToNetwork is true, it will try to schedule local synchronization using the synchronization adapter that is registered for the authority granted by the uri. No account will be transferred to the sync adapter, so all relevant accounts will be synced.

+2
09 Oct '15 at 11:03
source share

If your application is already running, try overriding the onNewIntent method

+1
Oct 29 '14 at 10:37
source share

There seems to be an easier way. In the OnMessageReceived method for the GCM listener, you can simply upgrade from there instead of sending a notification. You can use the same code that you would use when processing the notification. If you are using StartActivity from a listener, you should use the ActivityFlags.NewTask flag.

0
Sep 15 '16 at
source share

To summarize one sentence: If you want to update an activity, broadcast your custom event when a notification arrives and register your activity as a broadcast receiver of this event

0
Sep 21 '16 at 19:43
source share
 Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setAction(Long.toString(System.currentTimeMillis())); PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
-3
Mar 07 '14 at 2:35
source share



All Articles