Jetpack Navigation
的頁面跳轉(zhuǎn)讓我們可以方便的開發(fā)出單 Activity
多 Fragment
的應(yīng)用传睹。
以前多 Activity
的時(shí)候收到通知之后往往是點(diǎn)擊跳轉(zhuǎn)到指定 Activity
耳幢,現(xiàn)在變成單 Activity
多 Fragment
之后不知道怎么跳轉(zhuǎn)了,其實(shí)Google
提供了 DeepLink
來專門處理這種問題欧啤。
我們項(xiàng)目中用到了極光睛藻,在收到自定義消息之后,我們需要彈出一個(gè)通知
private void sendNotification(Context context, CustomMessage message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
channel.setDescription("description");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_avatar)
.setContentTitle(message.title)
.setContentText(message.message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(getPendingIntent(context, message))
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(new Random().nextInt(10000), builder.build());
}
這里基本和我們以前處理的一樣邢隧,只是構(gòu)造 PendingIntent
這里有點(diǎn)區(qū)別:
private PendingIntent getPendingIntent(Context context, CustomMessage message) {
NoticeType noticeType = new Gson().fromJson(message.extra, NoticeType.class);
// String type = noticeType.type;
int destId = R.id.articleDetailFragment;
Bundle bundle = new Bundle();
bundle.putString(ArticleDetailFragment.ID, noticeType.id);
PendingIntent pendingIntent = new NavDeepLinkBuilder(context)
.setGraph(R.navigation.main_navigation)
.setDestination(destId)
.setArguments(bundle)
.createPendingIntent();
return pendingIntent;
}
- 這里注意一下
destId
是需要跳轉(zhuǎn)的Fragment Id
店印, 不是Navigation
里面的Action Id
。
之后可能還會(huì)遇到問題倒慧,比如每次打開通知頁面之后返回以前的界面都會(huì)重新構(gòu)建一次按摘,感覺沒有類似的回退棧。這個(gè)問題 Google
也提供了解決辦法
顯式深層鏈接是深層鏈接的一個(gè)實(shí)例纫谅,該實(shí)例使用
PendingIntent
將用戶轉(zhuǎn)到應(yīng)用內(nèi)的特定位置炫贤。例如,您可以在通知付秕、應(yīng)用快捷方式或應(yīng)用微件中顯示顯式深層鏈接兰珍。當(dāng)用戶通過顯式深層鏈接打開您的應(yīng)用時(shí),任務(wù)返回堆棧會(huì)被清除询吴,并被替換為相應(yīng)的深層鏈接目的地掠河。當(dāng)嵌套圖表時(shí),每個(gè)嵌套級(jí)別的起始目的地(即層次結(jié)構(gòu)中每個(gè)
<navigation>
元素的起始目的地)也會(huì)添加到相應(yīng)堆棧中猛计。也就是說唠摹,當(dāng)用戶從深層鏈接目的地按下返回按鈕時(shí),他們會(huì)返回到相應(yīng)的導(dǎo)航堆棧有滑,就像從入口點(diǎn)進(jìn)入您的應(yīng)用一樣跃闹。
具體參考 點(diǎn)我