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

问题描述 投票:0回答:3

其实我想知道应用程序是从通知点击打开或从应用程序图标点击打开,我从通知点击通过一些参数。

 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屏幕并将其传递给受尊重的活动,并打开此活动,当我按下关闭应用程序时,现在应用程序处于后台状态,当我从后台恢复应用程序时它将显示通知活动,

android notifications splash-screen
3个回答
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
}

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

0
投票

首先,当您将意图发送到启动屏幕时,将Flags添加到下面的意图中

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

© www.soinside.com 2019 - 2024. All rights reserved.