如何通过通知或应用图标点击android来了解应用程序打开?

如何通过通知或应用图标点击android来了解应用程序打开?,android,notifications,splash-screen,Android,Notifications,Splash Screen,实际上我想知道应用程序是从通知点击打开的,还是从应用程序图标点击打开的,我通过通知点击传递一些参数 Intent notificationIntent = new Intent(context, SplashScreen.class); notificationIntent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE); notificationIntent.putExtra("firebas

实际上我想知道应用程序是从通知点击打开的,还是从应用程序图标点击打开的,我通过通知点击传递一些参数

 Intent notificationIntent = new Intent(context, SplashScreen.class);
        notificationIntent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
        notificationIntent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
        notificationIntent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));
这些数据进入启动屏幕,并将其传递给受尊重的活动,然后打开此活动,当我按back键关闭应用程序时,现在应用程序处于后台状态,当我从后台恢复应用程序时,它将显示通知活动,

您可以检查:

 Intent intent = new Intent(this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
intent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
intent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));

PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId + 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this, "my_channel_01").setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(getString(R.string.app_name)).setContentText(remoteMessage.getData().get("title")).setContentIntent(pendingIntent1);

mBuilder1.setAutoCancel(true);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         mBuilder1.setChannelId("my_channel_01");

         CharSequence name = "My New Channel";                   // The user-visible name of the channel.
         int importance = NotificationManager.IMPORTANCE_HIGH;

         NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance); //Create Notification Channel
         mNotificationManager1.createNotificationChannel(channel);
  }
  mNotificationManager1.notify((int) System.currentTimeMillis(), mBuilder1.build());
if (!wasLaunchedFromRecents() && (myIntentData.getStringExtra("IS_FROM_NOTIFICATION").equals(Utility.NOTI_TYPE_1))) {
        // from notification
    }

private fun wasLaunchedFromRecents(): Boolean {
    return getIntent().flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY === Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
}

首先,当您将数据发送到意图启动屏幕时,向意图添加标志,如下所示

Intent notificationIntent = new Intent(context, SplashScreen.class);
        notificationIntent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
        notificationIntent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
        notificationIntent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

然后,在Backpressed上的notification activity(通知活动)中,不要调用finish()

当您从后台显示其他活动而不是通知活动时,有什么问题?问题是单击notification(通知)后打开通知活动,然后关闭应用并从最近的应用打开它将打开通知活动这不是偶然的当我从最近的应用打开它将打开我的默认活动,未打开通知活动我已尝试此操作,但当我从最近的应用程序返回时,我也会获得IS_from_通知值它是否有值?是的,我在问题中定义了通过值,并且我也从最近的应用程序返回了值,只有应用程序图标单击启动应用程序我无法获得值当获取值时,您删除getIntent()。removeExtra(“是否来自通知”)