【问题标题】:How to handle Pending intent from notification如何处理通知中的待处理意图
【发布时间】:2019-12-04 13:16:05
【问题描述】:

如果我有一个应用程序有

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

【问题讨论】:

  • 如果您想增加问题被回答的机会,我建议您包含一段示例代码来演示您的问题,以便回答的人可以提出更正建议,而不是从头开始编写所有内容。
  • @IcedLance 感谢提醒,我忘了添加我的代码

标签: android android-intent android-activity kotlin android-notifications


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2013-02-03
    相关资源
    最近更新 更多