Как узнать приложение, открытое из уведомления или значка приложения, нажмите Android? - PullRequest
0 голосов
/ 20 марта 2019

На самом деле я хочу знать, что приложение открывается по щелчку уведомления или открывается по щелчку значка приложения, я передаю некоторый параметр из щелчка уведомления, как.

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

и эти данные попадают на экран Splash и передаются в нужное действие, а затем открывают это действие, когда я снова нажимаю закрыть приложение, теперь приложение находится в фоновом состоянии, когда я возобновляю приложение из фона, оно будет отображать действие уведомления,

Ответы [ 3 ]

1 голос
/ 20 марта 2019

Вы можете проверить:

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
}
0 голосов
/ 20 марта 2019

Прежде всего, когда вы отправляете данные с намерением на заставку, добавьте флаги к намерению, как показано ниже

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

0 голосов
/ 20 марта 2019
 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());
...