【发布时间】:2016-12-27 17:22:28
【问题描述】:
我正在尝试呈现由不在正常应用程序流中的特殊活动处理的通知,并尝试让后台堆栈处理“正确”,意思是:
- 如果在应用程序运行时处理通知,则通知活动应该出现在当前堆栈上,从通知返回时应该离开我们在应用程序中的位置。请注意,这可能意味着应用程序已打开。
- 如果在应用程序未运行时处理通知,则通知活动应出现在应用程序的主(初始)活动中。
到目前为止,我用来呈现通知的代码是:
/**
* Show (or update) a notification for the current message set.
*
* @param showNotification true to use a high priority notification which will be immediately
* displayed (as opposed to just added to the status bar)
*/
private void createOrUpdateNotification(boolean showNotification) {
Message oldest = messages.get(0);
Message newest = messages.get(messages.size() - 1);
// Create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Set notification data and appearance
.setContentTitle(title)
.setContentText(newest.message)
.setSmallIcon(smallIcon)
.setWhen(newest.when)
.setColor(ContextCompat.getColor(context, R.color.primary_color))
// Set notification options
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(showNotification ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_LOW)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setOnlyAlertOnce(!showNotification);
// Set up the action if the first (oldest) message has an intent builder
if(oldest.intent != null) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context.getApplicationContext());
stackBuilder.addNextIntent(oldest.intent);
builder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
}
NotificationManagerCompat.from(context).notify(notificationId, builder.build());
Log.i("notification created");
}
为了澄清,Message.intent 是一个单一的意图,配置为打开通知处理活动。
我的问题是,如果应用程序当前正在运行并在打开通知时打开,则应用程序关闭并且通知显示在一个空堆栈上并且应用程序的后堆栈被清除。
我的理解是,如果内容意图是包含单个意图的待处理意图,则所需的行为应该是自动的,这里就是这种情况。
我错过了什么?
【问题讨论】:
-
我是否正确理解了您的问题?您想显示通知并在显示通知时停止发出通知的线程?这就是你想要的吗?
-
没有。通知由服务生成。内容操作会打开一个超出正常应用程序流程的活动。当该活动被解除时,我想回到被中断的拥有应用程序活动。
-
你解决了吗?我有同样的问题。
标签: android android-notifications back-stack