如何解决待处理的 intent 警告

How to fix pending intent warning

提问人:chanthini begam 提问时间:12/29/2022 最后编辑:Grant Birchmeierchanthini begam 更新时间:12/30/2022 访问量:500

问:

缺少可变性标志 --->这是我的 android kotlin 项目中的警告。我在其中使用提醒通知。在这种情况下,在设置待处理的意图时,如果我将 0 设置为待处理的意图内容 [val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)],则会出现缺少“可变性标志”警告。 如果我使用待处理的意图。FLAG_IMMUTABLE,在Google Play管理中心发布应用程序包后,我的项目出现错误。error--- [强烈建议使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 是可变的时才使用 FLAG_MUTABLE,例如,如果它需要与内联回复或气泡一起使用]。我应该在通知中使用什么。我使用的是 API 级别 26。谁能帮我。PendingIntentPendingIntent

    private fun createNotification(title: String, description: String) {
    val s = "Messages"

    val intent = Intent(this, MainActivity::class.java)

    intent.putExtra("fragmsg", s)
    Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK

    val pendingIntent =
        PendingIntent.getActivity(this, 0, intent, 0)

    val notificationManager =
        applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel =
            NotificationChannel("101", "channel", NotificationManager.IMPORTANCE_HIGH)
        notificationManager.createNotificationChannel(notificationChannel)
        notificationChannel.lightColor = Color.BLUE
        notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
      //  notificationChannel.enableVibration(true)

    }

    val notificationBuilder  =NotificationCompat.Builder(applicationContext, "101")
        .setContentTitle(title)
        .setContentText(description)
        .setSmallIcon(R.drawable.logof)
        .setPriority(PRIORITY_HIGH)
        .setAutoCancel(true)
        .setOngoing(false)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentIntent(pendingIntent)
        .setDefaults(Notification.DEFAULT_SOUND)

    notificationManager.notify(1, notificationBuilder.build())

}
Android 警告不 可变性 android-pendingintent

评论

0赞 Felipe Palma 12/29/2022
也许这个链接可以帮助你:stackoverflow.com/questions/67045607/......如果您使用 firebase,请尝试更新它。

答:

1赞 sajidjuneja 12/29/2022 #1
val pendingIntent =
        PendingIntent.getActivity(
   this,
   0,
   intent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

根据这里的文档: https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

相应地选择您的旗帜。

如果你想阅读更多关于这方面的信息,我建议你在这里阅读这篇很棒的文章: https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619

评论

0赞 chanthini begam 1/24/2023
出现了同样的问题。当我使用上面一个时,它不是固定的。请问有任何其他方法可以解决这个悬而未决的意图问题吗?