Refreshing Activity on Receiving Gcm Push Notification

Refreshing activity on receiving gcm push notification

Took me a few hours to figure it out. Posting here in case anyone anyone else has the same problem.

The idea is that you have to register your activity as a broadcast receiver. The easiest way to do this is like so:

//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 in the activity that you want to 'listen' for events.

Now, how do we send data to this 'listener'? Go to your push notification handler(or from where 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 running/open to receive the broadcast intent

Note2: I switched to a library called 'otto'. It does actually the same thing but easier, 'broadcasts events' thoughout the app. Here's a link http://square.github.io/otto/

Refresh layout when get push notification

You can make user of LocalBroadcastManager

ReceiverActivity.java

An activity that watches for notifications for the event named "custom-event-name".

@Override
public void onCreate(Bundle savedInstanceState) {

...

// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};

@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}

SenderActivity.java

The second activity that sends/broadcasts notifications.

@Override
public void onCreate(Bundle savedInstanceState) {

...

// Every time a button is clicked, we want to broadcast a notification.
findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}

// Send an Intent with an action named "custom-event-name". The Intent sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting 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);
}

You can send message(SecondActivity.java) from the onMessage method of your notification receiver.

refresh activity from service after notification

You have to use BroadcastReciever for this task:http://developer.android.com/reference/android/content/BroadcastReceiver.html

in Activity:

public class MainActivity extends Activity {
public static final String NOTIFY_ACTIVITY_ACTION = "notify_activity";
private BroadcastReciver broadcastReciver;

@Override
protected void onStart() {
super.onStart();
broadcastReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction.equals(NOTIFY_ACTIVITY_ACTION ))
{
//to do smth
}
}
}

IntentFilter filter = new IntentFilter( NOTIFY_ACTIVITY_ACTION );
registerReceiver(broadcastReciver, filter);
}

@Override
protected void onStop()
{
unregisterReceiver(broadcastReciver);
}

}

In Service:

Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.NOTIFY_ACTIVITY_ACTION );
broadcastIntent.putExtra("addtional_param", 1);
broadcastIntent.putExtra("addtional_param2", 2); //etc

sendBroadcast(broadcastIntent);

UPDATE

BTW It's better use LocalBroadcastManager for send broadcast inside the app. It uses the same way as normal broadcast, but first you create LocalBroadcastManager:

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(MainActivity.this);

and in the onStart:

manager.registerReciever(broadcastReciver, filter);

and in the onStop:

manager.unregisterBroadcast(broadcastReciver);

and in the service:

manager.sendBroadcast(broadcastIntent);

Refreshing an activity every 15 minutes in background and send a notification even if the application is closed

The proper method to do this is to use a JobScheduler. You can find the documentation here. You set it to fetch data from your web service every 15 minutes. Save this data to some persistent cache and have this activity load the data from cache everytime its started



Related Topics



Leave a reply



Submit