【问题标题】:How to open a fragment when user click the notification in Kotlin?用户单击 Kotlin 中的通知时如何打开片段?
【发布时间】:2021-08-15 06:39:54
【问题描述】:

如果用户单击通知,我会尝试打开片段而不是活动

这里是通知功能:

private fun sendNotification(nextPray: Pray) {
    log("Send Notification")
    //small view
    val collapsedView = RemoteViews(
        packageName,
        R.layout.notification_collapsed
    )
    //big view
    val expandedView = RemoteViews(
        packageName,
        R.layout.notification_expanded
    )
    val snoozeIntent = Intent(this, StopAlarm::class.java)
    val snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)

    val notificationIntent = Intent(this, FullAzan::class.java)
    val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

    val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID2)
            .setSmallIcon(R.drawable.qalby_ic)
            .setCustomContentView(collapsedView)
            //.setCustomBigContentView(expandedView)
            .setContentTitle("It's Time for ${nextPray.name}")
            .setContentText("Let's Pray")
        
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .addAction(
                R.drawable.ic_stop,
                getString(R.string.text_stop),
                snoozePendingIntent
            )
            .setChannelId(CHANNEL_ID2)
            .setAutoCancel(true)
    val notification = mBuilder.build()
    notificationManager = NotificationManagerCompat.from(this)
    notificationManager.notify(100, notification)
    //startForeground(100, notification)
    log("vibrating")
    vibrate()
    log("Azan Audio Started")
    MediaPlayerManager.getInstance(applicationContext).azan(nextPray)

    
}

在这些行中:

val notificationIntent = Intent(this, FullAzan::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

我初始化了它

并在这一行中调用它:

.setContentIntent(pendingIntent)

我想用片段而不是当前的活动替换“Intent(this, FullAzan::class.java)”,这样做的正确方法是什么?

【问题讨论】:

  • 您需要自己处理这个问题,例如,在notificationIntent 上添加一个额外的内容,您可以在FullAzan 中阅读,然后决定显示哪个Fragment
  • 你能说得更具体些吗,因为我是 kotlin 新手

标签: java android kotlin notifications fragment


【解决方案1】:

您不能使用Intent 直接打开fragment,而是应该打开片段的包含Activity 并传递一些额外的意图来指示要打开哪个片段,然后根据值显示适当的片段意图额外。例如,如果你想打开一个在 FullAzan Activity 中加载的片段

val notificationIntent = Intent(requireContext(), FullAzan::class.java)

// Add an extra in intent indicating the fragment to open
notificationIntent.putExtra("FRAGMENT_NAME", "SOME_FRAGMENT")
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

现在当FullAzan通过通知点击打开时,只需检查您要打开哪个片段,然后加载该片段

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    when(intent?.getStringExtra("FRAGMENT_NAME")){
         "SOME_FRAGMENT" -> loadSomeFragment()
         else -> // do something else
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多