推送通知无法正常工作 [英] Push Notification Not Works Properly

查看:87
本文介绍了推送通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android上的聊天应用程序上创建了.

I have created on Chatting Application in Android.

在推送通知中出现问题.

In that I have problem occurs in Push Notification.

问题是:

  • 当用户A向用户X发送消息时,X会获得推送.
  • 该用户B向用户X发送消息后,X便获得了推送的第二次.

然后,用户X单击推入用户A聊天屏幕"不会打开,但会单击推入用户B聊天屏幕".

Then User X clicks on Push of User A Chat Screen not opens, but clicks on Push of User B Chat Screen opens.

如何解决此问题.

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

谢谢!

推荐答案

您的问题在这里:

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 (包含最新的一组"extras").

This code overwrites the PendingIntent every time. So you will only ever have one PendingIntent (containing the most recent set of "extras").

您需要确保为每个不同的聊天会话生成一个不同的 PendingIntent .要使 PendingIntent 唯一,可以生成唯一的"Intent操作",也可以使用唯一的 requestCode 参数.

What you need is to ensure that for each different chat session, you generate a different PendingIntent. To make the PendingIntent unique, you can either generate a unique "Intent action" or use a unique requestCode parameter.

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

if we assume that from_id is a unique integer for each session, you could use that as the requestCode, like this:

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 是唯一字符串,则可以将其用作意图操作",如下所示:

or if username is a unique string, you could use that as the "Intent action" like this:

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 .

either of these solutions will ensure that you generate a separate PendingIntent for each session.

这篇关于推送通知无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆