【问题标题】:Message is received several times from gcm多次从 gcm 收到消息
【发布时间】:2015-07-22 23:46:11
【问题描述】:

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

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

【问题讨论】:

  • 您提到启动应用程序后再次触发相同的通知。听起来这个问题可能与您的 GCM 广播接收器有关。你能发布与此相关的代码吗?
  • 是的。刚贴出来。我正在使用标准 GcmListenerService 来接收通知。
  • 检查您的安卓应用是否在每次应用启动时再次注册(获取新令牌)?并检查您的服务器应用程序是否有新的客户端令牌(设备 ID)然后重新发送消息?

标签: android google-cloud-messaging


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试将标志设置为 FLAG_ACTIVITY_CLEAR_TOP

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      相关资源
      最近更新 更多