推送通知无法正常工作

时间:2015-02-03 06:17:37

标签: android push-notification android-pendingintent

我已在Android上的聊天应用程序中创建。

因为推送通知中出现问题。

问题是:

  • 当用户A向用户X发送消息时,X将获得推送。
  • 之后,用户B向用户X发送消息,因此X获得推送的第二个。

然后用户X点击推送用户A聊天屏幕未打开,但点击推送用户B聊天屏幕。

如何解决此问题。

    PendingIntent contentIntent;
    UserSessionManager userSession = new UserSessionManager(context);
    if (type.equals("2"))
    {
        if (userSession.getUserSession() != null = PendingIntent.getActivity(
                    context,
                    0,
                    new Intent(context, ChatActivity.class)
                            .putExtra("to_id", from_id)
                            .putExtra("username", username)
                            .putExtra("to_avt", to_avt)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                            | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }
        else
        {
            contentIntent = PendingIntent.getActivity(context, 0, new Intent(context,
                    SplashActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }

    }

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(Integer.parseInt(from_id), mBuilder.build());

感谢!!!

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

contentIntent = PendingIntent.getActivity(
                context,
                0,
                new Intent(context, ChatActivity.class)
                        .putExtra("to_id", from_id)
                        .putExtra("username", username)
                        .putExtra("to_avt", to_avt)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                        | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

此代码每次都会覆盖PendingIntent。所以你只会有一个PendingIntent(包含最新的"额外内容")。

您需要确保为每个不同的聊天会话生成不同的PendingIntent。要使PendingIntent唯一,您可以生成一个独特的"意图动作"或使用唯一的requestCode参数。

如果我们假设from_id是每个会话的唯一整数,您可以将其用作requestCode,如下所示:

contentIntent = PendingIntent.getActivity(
                context,
                from_id, // unique requestCode
                new Intent(context, ChatActivity.class)
                        .putExtra("to_id", from_id)
                        .putExtra("username", username)
                        .putExtra("to_avt", to_avt)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                        | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

或如果username是唯一字符串,您可以将其用作" Intent操作"像这样:

contentIntent = PendingIntent.getActivity(
                context,
                0,
                new Intent(context, ChatActivity.class)
                        .setAction(username) // Unique ACTION
                        .putExtra("to_id", from_id)
                        .putExtra("username", username)
                        .putExtra("to_avt", to_avt)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                        | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

这些解决方案中的任何一个都将确保您为每个会话生成单独的PendingIntent

相关问题