我使用一个Intent来在用户点击推送通知时启动。我遇到了一些在pending intent中传递额外信息的问题。当我这样做时:
Intent i = new Intent(this, DashboardActivity.class);
i.putExtra("pending", true);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
我期望的行为是:
- 待处理的意图会启动DashboardActivity
- 在DashboardActivity中检查额外信息
- 如果有额外信息,跳转到B活动页面
然而,实际发生的是:
- 待处理的意图会启动DashboardActivity
- 额外信息为空
以下是检查DashboardActivity中的额外信息的代码:
if (getIntent().getExtras() != null) {
Intent i = new Intent(DashboardActivity.this, B.class);
startActivity(i);
}
我不知道如何解决这个情况,如果有人可以帮助我,我将非常感激!
编辑:
private void sendNotification(String messageBody){
String id="",message="",title="";
if(type.equals("json")) {
try {
JSONObject jsonObject = new JSONObject(messageBody);
id = jsonObject.getString("id");
message = jsonObject.getString("message");
title = jsonObject.getString("title");
} catch (JSONException e) {
// }
}
}
else if(type.equals("message"))
{
message = messageBody;
}
Intent i = new Intent(this, DashboardActivity.class);
i.putExtra("pending", true);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.app_name));
notificationBuilder.setContentText(message);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(soundUri);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
notificationBuilder.setAutoCancel(true);
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}