关闭通知后打开应用程序主页

Open Application home page after dismissing notification

在打开通知后按下后退按钮,用户将返回到主屏幕,而不是返回到应用程序的主页。 (使用装有 Android 5.0 的三星 S5)

通知构建并显示如下:

NotificationManager mNotifyMgr =
                    (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(GcmMessageHandler.this, ListViewItemDetailActivity.class);
            Bundle b = new Bundle();
            //... put some data
            resultIntent.putExtras(b);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(GcmMessageHandler.this);
stackBuilder.addParentStack(ListViewItemDetailActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(GcmMessageHandler.this)
                            .setContentTitle("Notification")
                            .setSmallIcon(R.drawable.common_signin_btn_icon_dark)
                            .setAutoCancel(true)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                            .setContentIntent(resultPendingIntent)
                            .setPriority(0)
                            .setContentText(title);

mNotifyMgr.notify(mNotificationId++, mBuilder.build());

同样在清单文件中,我将parentActivity设置如下

<activity
        android:name=".ListViewItemDetailActivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
</activity>

您可以做的最简单的事情是从您的通知中传递 bool 变量,例如**(_Is_Coming_From_notification)** 并在您的 ListViewItemDetailActivity 活动中获取该变量,如果用户返回打开您的,则基于该变量应用主页。 下面是一些代码供您参考。

resultIntent.putExtra("is_Comming_Form_Notification", true);

在你的 activity 中获取它。

   boolean _Is_Comming_From_Notification = intent.getBooleanExtra("is_Comming_Form_Notification", false);

并在您的 BackPressed 方法中

 @Override
   public void onBackPressed() {

       if (_Is_Comming_From_Notification ) {
        Intent intent = new Intent(this, App_Home_Page.class);
        startActivity(intent);
       }

       super.onBackPressed();
   }