Reputation: 1313
I am trying to refresh the contents of an activity on click of a notification. I can navigate to the activity when I am in some other activity and I click on the notification. What I am trying to achieve is, I am in Activity A which is displaying some content. I get a new notification, I click on it Activity A should either be relaunched or the content in the activity should be refreshed with respect to what I am passing in the PendingIntent of the Notification.
What all I have done,
Tried setting PendingIntent.FLAG_CANCEL_CURRENT
and PendingIntent.FLAG_UPDATE_CURRENT
Tried setting Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP
in the intent I am passing along with the pending intent.
Checked the data in onNewIntent()
its doesn't get refreshed. I get the same data which I have passed in the old intent.
Passed a unique requestCode
along with the PendingIntent as well, still the same.
Any Other suggestions?
Upvotes: 6
Views: 7030
Reputation: 10971
try this code:
Intent notificationIntent = new Intent(context, DetailActivity.class);
notificationIntent.putExtra(CommonConstants.EXTRA_NOTE_ID, note.get_id());
notificationIntent.setAction(CommonConstants.NOTE_NOTIFICATION_REMINDER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name)).setContentText(note.getMessage());
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(note.get_id(), mBuilder.build());
most important you have to call
PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
or
PendingIntent.FLAG_CANCEL_CURRENT
Upvotes: 1
Reputation: 95568
If you use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
, this should recreate the activity if it was in the stack, or start a new activity if it wasn't in the stack. If you don't want the activity to be recreated (if it is already in the stack), you can use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
.
Upvotes: 6
Reputation: 328
As aldoran said, use LocalBroadcastManager. In your Activity class:
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(FILTER_STRING));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver", "Got message");
}
};
And then in GSM Broadcast put your data in intent:
Intent intent = new Intent(FILTER_STRING);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Upvotes: 4
Reputation: 790
You could also use LocalBroadcastManager
or write your own BroadcastReceiver
and then send Broadcast
from your Notification
Upvotes: -1