【发布时间】:2014-05-28 07:42:39
【问题描述】:
我正在使用 GCM 进行推送通知。我的限制是,从 GCM 服务器收到的包中,我必须将用户重定向到我的应用程序中的特定位置。当我只有一个通知时,每件事都运行良好。如果通知托盘中有两个通知,则用户将被重定向到基于最新捆绑包的活动。所有其他捆绑包都将被忽略。我关注了许多 SO 帖子,例如 this 和 this,但它并没有解决我的问题。以下是我的代码的摘录:
private void sendNotification(String msg, String type, String id) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("notification_type", type);
intent.putExtra("notification_id", id);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);//I have tried all flags of pending intent like PendingIntent.FLAG_UPDATE_CURRENT and other 3
// flag_update_current
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("RWD Rugby").setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
Random rnd = new Random();
NOTIFICATION_ID = rnd.nextInt(90000);
Log.d("notification number", NOTIFICATION_ID + "");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
在接收端我使用:
Intent intent = getIntent();//this gives me extras from the intent that started the activity not pending intent
String fromNotification = intent.getStringExtra("notification_type");
我可能不得不对可能得到的结果做出妥协。对我来说,最好的情况是根据我在创建意图时设置的属性将用户重定向到正确的页面。非常感谢任何帮助,如果我不够具体,请询问。
【问题讨论】:
-
@girish nair 你改了什么??
-
点击“Edited May 28 at 11:55”然后选择第三个选项,并排比较。
-
@downvoter 关心评论??
标签: android android-intent push-notification google-cloud-messaging android-pendingintent