从 gcm 多次收到消息
Message is received several times from gcm
收到通知后,我使用此代码发送广播。
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));
广播打开不同的活动,当应用是运行或不是
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);
}
}
当我点击通知时,应用 运行 一切正常。但是当我终止应用程序并打开通知时。 activity 按预期打开,但再次收到相同的通知。
这是gcm服务代码。
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);
}
}
尝试将标志设置为 FLAG_ACTIVITY_CLEAR_TOP
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
我发现了问题。我手动启动了 GcmListenerService,但我不应该这样做。删除 startService 代码后问题得到解决。
收到通知后,我使用此代码发送广播。
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));
广播打开不同的活动,当应用是运行或不是
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);
}
}
当我点击通知时,应用 运行 一切正常。但是当我终止应用程序并打开通知时。 activity 按预期打开,但再次收到相同的通知。
这是gcm服务代码。
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);
}
}
尝试将标志设置为 FLAG_ACTIVITY_CLEAR_TOP
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
我发现了问题。我手动启动了 GcmListenerService,但我不应该这样做。删除 startService 代码后问题得到解决。