首页 > 解决方案 > 如何处理通知中的待处理意图

问题描述

如果我有一个应用程序

A -> B -> C -> D 

在堆栈中。我怎么能从通知中打开一个活动并使堆栈成为

A -> B -> C -> D -> E

如果应用程序被终止,我如何从通知中打开应用程序,例如从通知中打开 E 活动,但在再次打开应用程序时阻止它启动 [例如,我打开 E 活动然后我回退以退出应用程序,然后当我再次打开应用程序时我希望它打开 A 活动(根活动)而不是 E 活动。]

// When Open Application if app is terminated.

val intent = Intent(activity!!, SampleActivity::class.java)
val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        with(NotificationManagerCompat.from(activity!!)) {

            notify(
                java.lang.System.currentTimeMillis().toInt(), notiBuilder
                    .setContentTitle("Title")
                    .setContentIntent(pendingIntent)
                    .setContentText("body")
                    .setNumber(1)
                    .setSmallIcon(R.drawable.notification_icon_background)
                    .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                    .build())

        }


// When Open Application if app is not terminated.

val intent = Intent(activity!!, HomeActivity::class.java)
intent.putExtra("FurtherActivity", 1)    
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK   

val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        with(NotificationManagerCompat.from(activity!!)) {

            notify(
                java.lang.System.currentTimeMillis().toInt(), notiBuilder
                    .setContentTitle("Title")
                    .setContentIntent(pendingIntent)
                    .setContentText("body")
                    .setNumber(1)
                    .setSmallIcon(R.drawable.notification_icon_background)
                    .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                    .build())

        }

// HomeActivity

override fun onCreate() {
    ......
    ......
    ......
    if(intent.getIntExtra("FurtherActivity", 0) == 1) {
        SampleActivity.start(this@HomeActivity)
        viewpager.currentItem = 0
        tab_layout.getTabAt(0)!!.select()
    }
    ......
    ......
}

override fun onNewIntent(newIntent: Intent?) {
    super.onNewIntent(Intent())
    if(intent.getIntExtra("FurtherActivity", 0) == 1) {
        SampleActivity.start(this@HomeActivity)
        viewpager.currentItem = 0
        tab_layout.getTabAt(0)!!.select()
    }
}    

标签: androidandroid-intentandroid-activitykotlinandroid-notifications

解决方案


您可以创建一个处理意图结果的活动,并将该结果根据要求传递给特定活动,然后只需将 HandleIntent 活动传递给通知意图,并使用 ActivityName 作为参数来打开特定活动。


推荐阅读