我从接收到的activity启动notification。如果按下该notification,它将启动一个activity。如果在activities上没有以前的back-stack,或者只有一个特定的activity,我希望删除这个特定的activity,并在其中插入我的主activity,而不是新的活动。
我找到了这个线程,但我不明白他是如何用两个意图和标志来处理它的。
即intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)
这样做是明智的,还是我应该为此编辑活动堆栈?
我对android相当陌生,所以一些建议可以帮助我解决这个问题。非常感谢;)
更新:所以我和stackbuilder一起去了,但是不知怎么的,它没有被设置好.我没有发现我的错误,我的布尔noActivity被确定地设置了,但是我想不知为什么我误解了堆栈实际上是如何将先前的一个活动放在那里的。
private void sendNotification(messageType type, Map<String, String> data, boolean noActivities) {
Intent i;
String message = "";
switch (type) {
case newFOLLOWER:
User cur = new User(data.get("other.name"));
User.lookAT = User.getOtherUserByName(cur);
i = new Intent(this, other_profile.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
message = data.get("other.name") + " is following you now. Click to see his profile";
i.putExtra("Notification", data.get("other.name") + " is following you now. Click to see his profile");
break;
default:
i = null;
break;
}
if (i != null) {
TaskStackBuilder stack = TaskStackBuilder.create(this);
if(noActivities){
stack.addParentStack(Photostream.class);
}
stack.addNextIntent(i);
PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("PIC LOC")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}更新2:在大量搜索之后,我发现了堆栈构建器是如何工作的。我找到了另一个线程,他们描述了添加是如何工作的。编辑报表以便有一个先前的堆栈。我很快就跳过了你给我的部分教程.
(谢谢你们的帮助;)
发布于 2017-07-12 11:15:29
在创建这样的通知时,您应该创建堆栈生成器。
Intent resultIntent = new Intent(this, NotificationTapActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);因此,每当用户点击通知时,MainActivity将被插入堆栈中。
还有一个解决方案,您可以在其中处理NotoificationTapActivity的后压。在其中,您可以检查堆栈中是否存在注意事项,然后就可以完成当前的活动并从那里启动MainActivity。
发布于 2017-07-12 10:59:52
您应该使用TaskStackBuilder。这是最有效的方法,也是为此而开发的TaskStackBuilder结构。
当我的项目中包含了TaskStackBuilder时,我总是使用Push Notifications。
覆盖onBackPress()不是一个好方法,因为它需要应用程序中复杂的结构,并且需要处理许多事情。
查看这教程。
发布于 2017-07-12 10:58:34
/**Just use below code inside onMessageReceived()**/
PendingIntent pendingIntent;
Uri defaultSoundUri;
Intent notification_intent;
String message = "Your Message";
notification_intent = new Intent(this, YourActivity.class);
notification_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 , notification_intent, PendingIntent.FLAG_ONE_SHOT);
defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon(notificationBuilder))
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
String[] multilineDescription = new String[]{message};
message = multilineDescription[0];
for (int i=1; i<multilineDescription.length; i++)
{
message += System.getProperty("line.separator");
message += multilineDescription[i];
}
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
/***Above commented is for strecting the big message in Push Notification******/
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)MESSAGE_NOTIFICATION_ID /* ID of notification */, notificationBuilder.build());https://stackoverflow.com/questions/45055606
复制相似问题