【发布时间】:2016-07-19 07:06:23
【问题描述】:
我有一个应用程序,我可以在其中收到 GcmListenerService 中的 GCM 推送通知(如 gcm android 示例中所示)。每当我收到通知时,我都会收到一些 JSON 与显示通知相关的数据。
现在假设我收到“有人为我添加书签”的通知,为此我获得了该用户的pk(标识符),当我点击通知时,它使用推送通知中收到的pk显示该用户的个人资料。
问题:如果我收到 2 个上述类型的通知,那么无论我点击哪个通知,我总是会转到我最近收到通知的人的个人资料。
在其他通知之上获取通知是否会覆盖先前通知的值,以便只有最近的通知有效?这是否意味着我只能显示来自应用程序的一个推送通知?如果需要,我可以发布代码。
相关代码:
@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");
}
}
}
其他内部代码:
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);
}
}
}
编辑 两个不同类型的通知(打开不同的活动)不会发生此问题,它仅发生在打开同一屏幕的通知时。
编辑 1 以下是推送通知中收到的值:
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}]
这个有notificationId 为4267,现在假设我收到了同一屏幕的另一个通知,notificationId 说,4268,然后当我记录在图像屏幕中收到的notificationId 时,我在打开两者时得到 4268通知。
编辑 2 我猜这个问题与 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.
这就是正在发生的事情。意向附加内容的内容被最新通知的意向附加内容覆盖。所以,我尝试使用FLAG_ONE_SHOT,但这样我就可以打开旧的意图,但是当我点击最新的意图时会打开。
我希望两个通知都在推送通知列表中,并且它们都应该在各自的屏幕上具有不同的意图附加值。
【问题讨论】:
-
您能否分享您的自定义 JSON 接收器的代码(如果有)?您可以在被最新推送覆盖的变量中设置目标值。
-
您必须根据响应中的通知类型来区分各种通知,例如:{msg_typ:"profile"} 或 {msg_type:"bookmark"},然后您可以导航到特定屏幕。
-
@SlashG 我会分享代码。
-
@SlashG,我添加了代码 sn-ps。请检查。
-
你在排队 Intents 吗?尝试覆盖
notificationOpened(或等效)方法以查看它包含的“ID”。此外,如果您正确接收数据,请记录。
标签: android notifications google-cloud-messaging gcmlistenerservice