我有两个活动MainActivity和ReceivedMessageActivity。以及从中获取Context的后台服务,并传递给扩展BroadcastReceiver以接收短信的SMSReceiver。当短信收到SMSReceiver创建通知时。我希望当我点击通知时,如果应用程序没有运行,则打开ReceivedMessageActivity,当应用程序运行时,如果ReceivedMessageActivity已打开,则不要按意图创建新的Activity,只需将ReceivedMessageActivity置于前面。我将manifest中的launchMode设置为singleTop,因为MainActivity正在工作,但是对于ReceivedMessageActivity不起作用!我将通知意图标志设置为FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP和FLAG_ACTIVITY_CLEAR_TOP,但是不起作用。我将ReceivedMessageActivity的manifest launchMode设置为singleInstance,但是当我手动接收ReceivedMessageActivity时,它的默认动画不起作用(从右到左动画)。这是我的通知代码:
public void addNotifiction(String title, String message) {
NotificationCompat.Builder builder = new
NotificationCompat.Builder(context);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setStyle(new
NotificationCompat.BigTextStyle().bigText(message));
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setSmallIcon(R.drawable.edit_icon);
builder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
Intent notifactionIntent = new Intent(context,
ReceivedMessagesActivity.class);
notifactionIntent
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(context);
taskStackBuilder.addNextIntentWithParentStack(notifictionIntent);
PendingIntent contentPendingIntent= taskStackBuilder.getPendingIntent (0,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentPendingIntent);
NotificationManager manager = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID++, builder.build());
}
我的上下文是MyService类上下文:
Context context=MyService.context
编辑:我发现当我使用android:parentActivityName=".MainActivity"
时
在manifestMessageActivity的manifest中发生了这种情况,当我点击通知并且receiveMessageActivity打开时我的app创建了新的receivedMessageActivity为什么?
答案 0 :(得分:0)
我的某个应用程序的通知制作者的基本框架希望它有所帮助:
Context context = getApplicationContext();
String title = context.getString(R.string.title);
String subtitle = context.getString(R.string.subtitle);
Notification.Builder notificationBuilder = null;
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getBaseContext(), ReceivedMessageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationBuilder = new Notification.Builder(
context,
CHANNEL_ID)
) //
.setContentTitle(title)
.setContentText(subtitle)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(
getBaseContext(),
0,
intent,
0
)
)
.setAutoCancel(true);
} else {
notificationBuilder = new Notification.Builder(context) //
.setContentTitle(title)
.setContentText(subtitle)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(
getBaseContext(),
0,
intent,
0
)
)
.setAutoCancel(true);
}
startForeground(NOTIFICATION_ID, notificationBuilder.build());// only in case you are in service
以下代码未经过测试