[英]How to know app open from notification or app icon click android?
實際上我想知道應用程序是從通知單擊中打開還是從應用程序圖標單擊中打開,我從通知單擊中傳遞了一些參數。
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));
然后這些數據進入“閃屏”屏幕,並將其傳遞到受關注的活動,然后打開此活動,當我后按關閉應用程序時,現在應用程序處於后台狀態,當我從后台恢復應用程序時,它將顯示通知活動,
您可以檢查:
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 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());
首先,當您以意圖發送數據到啟動屏幕時,將標志添加到意圖中,如下所示
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.