我正在显示附加到我的项目的库的通知,当点击通知时,通知将转到活动(ReceivingActivity)。单击通知后,活动将打开,但不会收到附加到其中的附加内容。
通知触发代码 - 当我收到gcm消息并且通知代码在库中时,我调用sendNotification
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent redirectIntent = new Intent(this, Class.forName("com.xyz.kljdf.ReceivingActivity"));
redirectIntent.putExtra("key", "value"); // These extras are not received in ReceivingActivity onCreate(), see the code below
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
redirectIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(((Context)this).getResources().getIdentifier("icon", "drawable", getPackageName()))
.setContentTitle(extras.getString(PushConstants.TITLE))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(extras.getString(PushConstants.ALERT)))
.setContentText(extras.getString(PushConstants.ALERT));
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
检查ReceivingActivity中的附加内容......
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiving);
Log.i("extras..... @@@@@@", getIntent().getExtras().toString()); //NPE here
}
我需要帮助来弄清楚如何传递额外内容并正确接收它们。
答案 0 :(得分:5)
确保PendingIntent在调用时实际传递数据。请参阅此答案以获得解释:https://stackoverflow.com/a/3851414/1426565
对于其他任何人来说,如果你肯定意图已经存在并且只是被带到了前面,那么新的Intent数据默认不会传递给它。你可以覆盖onNewIntent(Intent)以解决这个问题:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent); // Set the new Intent's data to be the currently passed arguments
}