我正在使用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");
我可能不得不对我可能得到的结果妥协。对我来说,最好的情况是根据我在创建意图时设置的属性将用户重定向到正确的页面。非常感谢任何帮助,如果我不够具体,请询问。
答案 0 :(得分:2)
您可以尝试以下代码.. (尝试使用FLAG_UPDATE_CURRENT)
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
// .setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}