通过PendingIntent实现直接跳转到某个activity或者fragment
eg:通过通知直接跳转到某个页面
private val CHANNEL_ID="channel_id"
binding.btnNotification.setOnClickListener {
setNotification()
}
private fun setNotification(){
if (activity==null){
return
}
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
val importance=NotificationManager.IMPORTANCE_DEFAULT
val channel=NotificationChannel(CHANNEL_ID,"channel_name",importance)
val notificationManager=activity?.getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannel(channel)
}
val builder=NotificationCompat.Builder(activity!!,CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("deeplink")
.setContentText("hello world")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(getPendingIntent())
.setAutoCancel(true)
val notificationManager=NotificationManagerCompat.from(activity!!)
notificationManager.notify(1,builder.build())
}
private fun getPendingIntent():PendingIntent?{
if (activity!=null){
val bundle=Bundle()
bundle.putString("PARAMS","PendingIntent")
return Navigation.findNavController(activity!!,R.id.btn_notification).createDeepLink()
.setGraph(R.navigation.mobile_navigation).setDestination(R.id.navigation_notifications)
.setArguments(bundle).createPendingIntent()
}
return null
}
R.id.navigation_notifications这个是一个通知的fragment
<navigation>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.app.servicedemo.navigation.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
接收的fragment的代码:
val bundle=arguments?.getString("PARAMS")
textView.text=bundle?:""
网友评论