如何根据将单击的推送通知的确切内容来选择将成为 startActivity 的 Activity ??? (科特林)

Posted

技术标签:

【中文标题】如何根据将单击的推送通知的确切内容来选择将成为 startActivity 的 Activity ??? (科特林)【英文标题】:How to select Activity which will be startActivity depending on what exactly push notification will be clicked??? (Kotlin) 【发布时间】:2021-10-23 15:36:24 【问题描述】:

所以,我有一个应用程序使用不同目的的推送通知。当您单击这些通知时,它将始终显示唯一的 Activity - SearchActivity,因为它位于 FirebaseMessagingService 类中的 onMessageRecieved fun 代码中:

private const val CHANNEL_ID = "my_channel"

 class FirebaseService: FirebaseMessagingService() 

    companion object 
        var sharedPref: SharedPreferences? = null

        var token: String?
            get() 
                return sharedPref?.getString("token", "")
            
            set(value) 
                sharedPref?.edit()?.putString("token", value)?.apply()
            
    

    override fun onNewToken(newToken: String) 
        super.onNewToken(newToken)
        token = newToken
    

    override fun onMessageReceived(message: RemoteMessage) 
        super.onMessageReceived(message)

        val intent = Intent(this, **SearchActivity::class.java**)
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationID = Random.nextInt()

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            createNotificationChannel(notificationManager)
        

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_ONE_SHOT)
        val notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(message.data["title"])
            .setContentText(message.data["message"])
            .setSmallIcon(R.drawable.gaz)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build()

        notificationManager.notify(notificationID, notification)
    

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(notificationManager: NotificationManager) 
        val channelName = "channelName"
        val channel = NotificationChannel(CHANNEL_ID, channelName, IMPORTANCE_HIGH).apply 
            description = "My channel description"
            enableLights(true)
            lightColor = Color.GREEN
        
        notificationManager.createNotificationChannel(channel)
    

问题是 - 如何在应用打开时根据点击的通知使用不同的 Activity 作为第一个 Activity?换句话说 - 当点击带有“text X”的推送通知时,我们会转到 Activity1,当点击带有“text Z”的推送通知时,我们会转到 Activity3,等等......

【问题讨论】:

message.data["message"]里面有:text x", "text z"的信息吗? 【参考方案1】:

你可以这样做:

override fun onMessageReceived(message: RemoteMessage) 
    super.onMessageReceived(message)
    val msg = message.data["message"]
    var intent = null
    when(msg)
    "text X": intent = Intent(this, SearchActivity1::class.java)
    "text Y": intent = Intent(this, SearchActivity2::class.java)
    "text Z": intent = Intent(this, SearchActivity3::class.java)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val notificationID = Random.nextInt()

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        createNotificationChannel(notificationManager)
    

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_ONE_SHOT)
    val notification = NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle(message.data["title"])
        .setContentText(message.data["message"])
        .setSmallIcon(R.drawable.gaz)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .build()

    notificationManager.notify(notificationID, notification)


@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(notificationManager: NotificationManager) 
    val channelName = "channelName"
    val channel = NotificationChannel(CHANNEL_ID, channelName, IMPORTANCE_HIGH).apply 
        description = "My channel description"
        enableLights(true)
        lightColor = Color.GREEN
    
    notificationManager.createNotificationChannel(channel)
  

【讨论】:

如果您也可以对答案进行投票,那就太好了,因为它可以帮助社区中遇到同样问题的其他人。谢谢。

以上是关于如何根据将单击的推送通知的确切内容来选择将成为 startActivity 的 Activity ??? (科特林)的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法启用推送通知,以便当用户单击它时,它会将他们带到与通知显示的内容相关的页面?

iOS 推送通知消息 - 单击查看按钮后的操作

推送通知指南?

Apple 推送通知 - 查看按钮操作

iOS - 未确定推送通知

单击推送通知的操作按钮时,如何转到最新的 DetailView?