Reputation: 4262
When notification is received I send a broadcast, using this code.
Event event = requestManager.getEventById(comment.eventId);
Intent intent = new Intent(NOTIFICATION_ACTION).putExtra(EventFragment.EVENT, event);
builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
The broadcast opens different activities, when the app is running or not.
public class NotificationBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Event event = intent.getParcelableExtra(EventFragment.EVENT);
if (event != null) {
if (MainActivity.isRunning()) {
Bundle args = new Bundle();
args.putParcelable(EventFragment.EVENT, event);
intent = OneFragmentActivity.getStartIntent(context, EventFragment.class, args, R.layout.toolbar);
} else {
intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.COMMENT_NOTIFICATION_EVENT, event);
}
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
}
When I click on the notification, when the app is running everything is working as expected. But when I kill the application and open the notification. The activity is opened as expected, but the same notification is received again.
Here is a gcm service code.
public class MyGcmListenerService extends GcmListenerService {
public static final String NOTIFICATION_ACTION = "com.khevents.Notification";
private void setupCommentNotification(Comment comment, Notification.Builder builder) throws IOException {
RequestManager requestManager = EventsApp.getInstance().getRequestManager();
VkUser vkUser = requestManager.getVkUserById(comment.userId);
builder.setLargeIcon(BitmapUtilities.getBitmapFromURL(vkUser.avatar));
builder.setContentTitle(vkUser.name + " " + vkUser.lastName);
builder.setContentText(comment.text);
Event event = requestManager.getEventById(comment.eventId);
Intent intent = new Intent(NOTIFICATION_ACTION).putExtra(EventFragment.EVENT, event);
builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
private Notification.Builder createNotification(GCMData data) throws IOException {
Notification.Builder builder = new Notification.Builder(this);
if (data.comment != null) {
setupCommentNotification(data.comment, builder);
}
builder.setSmallIcon(R.drawable.add_icon);
return builder;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(String from, Bundle bundle) {
super.onMessageReceived(from, bundle);
String json = bundle.getString("data");
Log.i("GCM", "message received: " + json);
GCMData data = Json.readNoThrow(json, GCMData.class);
if (data == null) {
return;
}
try {
Notification.Builder notification = createNotification(data);
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
postNotification(notification);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private void postNotification(Notification.Builder builder) {
Notifications.notify(this, 1, builder);
}
}
Upvotes: 0
Views: 227
Reputation: 4262
I found the problem. I started GcmListenerService manually, but I shouldn't do this. The problem was fixed after removing startService code.
Upvotes: 2
Reputation: 793
try to set flag to FLAG_ACTIVITY_CLEAR_TOP
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Upvotes: 0