推送通知未打开正确的屏幕

时间:2016-07-19 07:06:23

标签: android notifications google-cloud-messaging gcmlistenerservice

我有一个应用程序,我在GCM中获得GcmListenerService推送通知(如gcm android示例中所示)。每当我收到通知时,我会得到一些JSON,其中包含与显示通知相关的数据。

现在假设我收到一条通知,其中有人为我添加了书签,因为我获得了该用户的pk(标识符),当我点击通知时,它会使用{{ 1}}在推送​​通知中收到,以显示该用户的个人资料。

问题:如果我收到上述类型的2条通知,那么无论我点击哪个通知,我都会查看我最近收到通知的人的个人资料。

在其他通知之上获取通知是否会覆盖以前通知的值,以便只有最新通知才有效?这是否意味着我只能从应用中显示一个推送通知?如果需要,我可以发布代码。

相关代码:

pk

其他内部代码:

@Override
    public void onMessageReceived(String from, Bundle data) {
        tinyDB = new TinyDB(this);
        if (tinyDB.getBoolean(AppConstants.LOGIN_STATE, false)) {
            try {
                Log.i("NOTIF", data.toString());
                String screen = data.getString("screen");
                String dataJson = data.getString("data");
                String displayJson = data.getString("display");
                String notification_id = data.getString("notif_id");
                String priority = data.getString("priority");
                String style = data.getString("style");

                Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                NotificationVal notificationVal = gson.fromJson(displayJson, NotificationVal.class);

                String title = notificationVal.getTitle();
                String text = notificationVal.getText();
                String largeText = notificationVal.getLargeText();
                String smallIcon = notificationVal.getSmallIcon();
                String largeIcon = notificationVal.getLargeIcon();

                Bitmap smallImage = null;
                Bitmap largeImage = null;

                if (!TextUtils.isEmpty(smallIcon)) {
                    smallImage = getBitmapFromURL(smallIcon);
                }

                if ("big_picture".equalsIgnoreCase(style) && (largeImage != null)) {
                    NotificationCompat.BigPictureStyle notificationStyle = new NotificationCompat.BigPictureStyle();
                    notificationStyle.setBigContentTitle(title);
                    notificationStyle.setSummaryText(text);
                    notificationStyle.bigPicture(largeImage);

                    Intent intent = NotificationIntentBuilder.get(this, dataJson, screen, smallIcon, largeIcon, notification_id);

                    TaskStackBuilder stackBuilder = NotificationStackBuilder.get(this, screen, dataJson);
                    stackBuilder.addNextIntent(intent);

                    PendingIntent resultPendingIntent =
                            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

                    mBuilder.setSmallIcon(R.drawable.white_square);
                    mBuilder.setAutoCancel(true);
                    mBuilder.setColor(Color.parseColor("#fac80a"));
                    mBuilder.setLargeIcon(largeImage);
                    mBuilder.setContentTitle(title);
                    mBuilder.setContentText(text);
                    mBuilder.setContentIntent(resultPendingIntent);
                    mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
                    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
                    mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
                    mBuilder.setStyle(notificationStyle);

                    mBuilder.setContentIntent(resultPendingIntent);

                    NotificationManager mNotificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    if (notification_id != null) {
                        mNotificationManager.cancel(Integer.parseInt(notification_id));
                        mNotificationManager.notify(Integer.parseInt(notification_id), mBuilder.build());
                    }
                } else {
                    Log.i("GCM", "Dummy Notification");
                }
            } catch (Exception e) {
                Log.i("NOTIF", "An exception occurred");
            }
        }
    }

编辑两个不同类型的通知(打开不同的活动)不会发生此问题,只会在通知打开同一屏幕时发生。

编辑1 以下是推送通知中收到的值:

public class NotificationStackBuilder {
    public static TaskStackBuilder get(Context context, String screen, String data) {
        if ("IMAGE".equalsIgnoreCase(screen)) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(ImageActivity.class);
            return stackBuilder;
        } else {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(NewHomeActivity.class);
            return stackBuilder;
        }
    }
}

public class NotificationIntentBuilder {
    public static Intent get(Context context, String data, String screen, String smallIcon, String largeIcon, String notificationId) {
        if ("IMAGE".equalsIgnoreCase(screen)) {
            String pk = "";
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(data);
                if (jsonObject.has("pk")) {
                    pk = jsonObject.getString("pk");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(context, ImageActivity.class);
            intent.putExtra("ID", pk);
            intent.putExtra("notificationId", notificationId);
            return intent;
        } else if ("GALLERY".equalsIgnoreCase(screen)) {
            String pk = "";
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(data);
                if (jsonObject.has("pk")) {
                    pk = jsonObject.getString("pk");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(context, GalleryActivity.class);
            intent.putExtra("ID", pk);
            intent.putExtra("notificationId", notificationId);
            return intent;
        } else {
            return new Intent(context, NewHomeActivity.class);
        }
    }
}

这个有Bundle[{google.sent_time=1469254984538, priority=0, screen=IMAGE, data={"pk":650}, style=big_picture, google.message_id=0:1469254984541527%e07f0e28f9fd7ecd, notif_id=4267, display={"large_icon":"https:\/\/d2r0rrogy5uf19.cloudfront.net\/photos\/971c03f8-6a5d-30a3-9e49-0ab416cb8fa0.jpg","small_icon":"","text":"Random hi5'd your photo to 'Diwali'","title":"random","large_text":""}, collapse_key=do_not_collapse}] 为4267,现在假设我收到同一个屏幕的另一个通知,notificationId说4268,然后当我记录图像屏幕中收到的notificationId时,打开两个通知我得到4268。

编辑2 我想这个问题与notificationId有关。以下是文档中的内容:

PendingIntent.FLAG_UPDATE_CURRENT

这就是发生的事情。意图附加内容被最新通知的意图附加内容覆盖。所以,我尝试使用Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. ,但是我可以打开旧的意图,但是当我点击最新的意图时会注意到。

我希望这两个通知都在推送通知列表中,并且它们都应该在具有不同意图附加值的相应屏幕上。

1 个答案:

答案 0 :(得分:1)

您需要为每个通知提供唯一的请求代码,否则它将被替换为新的通知。

摘自PendingIntent documentation

  

数组中的最后一个意图表示PendingIntent的键。   换句话说,它是匹配的重要元素(如已完成   给getActivity的单一意图(Context,int,Intent,   int),其内容将成为send的替换主题(Context,   int,Intent)和FLAG_UPDATE_CURRENT等。这是因为它是   最具体的提供意图,以及用户实际的UI   看到意图何时开始。

所以请替换以下行:

PendingIntent resultPendingIntent = stackBuilder. getPendingIntent(0,
                                        PendingIntent.FLAG_UPDATE_CURRENT);

假设notification_id是唯一整数,只需将其替换为如下:

PendingIntent resultPendingIntent =
          stackBuilder.getPendingIntent(Integer.parseInt(notification_id)
              , PendingIntent.FLAG_UPDATE_CURRENT);