我使用的是当用户点击推送通知时启动的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的附加功能的代码:
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());
}
答案 0 :(得分:1)
尝试使用PendingIntent.FLAG_UPDATE_CURRENT
。
同时添加Intent.FLAG_ACTIVITY_SINGLE_TOP
,这样如果您的活动已经打开,则不会重新创建。
将上述2个标志添加到您的意图中。
同样在您的DashboardActivity实现onNewIntent
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("pending"))
{
}
}