从gcm收到多次消息

时间:2015-07-22 23:46:11

标签: android google-cloud-messaging

收到通知后,我使用此代码发送广播。

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);
    }
}

当我点击通知时,当应用程序运行时,一切都按预期工作。但是当我杀死应用程序并打开通知时。活动按预期打开,但会再次收到相同的通知。

这是一个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);
    }
}

2 个答案:

答案 0 :(得分:2)

我发现了问题。我手动启动了GcmListenerService,但我不应该这样做。删除startService代码后问题得到解决。

答案 1 :(得分:0)

尝试将标志设置为FLAG_ACTIVITY_CLEAR_TOP

 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);