如何从通知或应用程序图标单击android知道应用程序打开?

时间:2019-03-20 06:42:20

标签: android notifications splash-screen

实际上,我想知道从通知单击中打开应用程序还是从图标单击中打开应用程序,我从通知单击中传递了一些参数。

 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));

,此数据进入“启动”屏幕,并将其传递到受关注的活动,然后打开此活动,当我后按关闭应用程序时,现在应用程序处于后台状态,当我从后台恢复应用程序时,它将显示通知活动,

3 个答案:

答案 0 :(得分:1)

您可以检查:

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
}

答案 1 :(得分:0)

 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());

答案 2 :(得分:0)

首先,当您将数据发送到初始屏幕时,如下所示将标志添加到目标中

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);

然后在onBackPressed的通知活动中不要调用finish()