当用户点击通知时,它将打开新活动即可正常工作。但当我按下后退按钮时它关闭了应用程序
我想要什么? 当我单击“返回”按钮时,它将返回MainActivity(Selected Activity)Everytim。
private void generateNotificationNew(Context context, String message,String pushIdmessage) {
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,ResultActivity.class);
//Intent resultIntent = new Intent(this, AvailableJobActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
stackBuilder.addParentStack
或stackBuilder.addNextIntent
可能无法正常工作。
任何替代选项谢谢。
答案 0 :(得分:1)
对于生成通知,您需要输入以下代码...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title")
.setContentText("message");
Intent notificationIntent = new Intent(this , ResultActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0 , builder.build());
对于回退事件,您需要检查结果活动
中 onBackPressed()中的以下内容@Override
public void onBackPressed() {
if(this.isTaskRoot())
startActivity(new Intent(this , MainActivity.class));
super.onBackPressed();
}
现在您可以从通知开始活动重定向到MainActivity。 享受 ......: - )