【问题标题】:Notifications only work on certain versions of android通知仅适用于某些版本的 android
【发布时间】:2023-02-05 20:52:27
【问题描述】:

我的应用程序使用接收器在一定时间后向用户发送通知。接收器运行良好,因为它运行一些功能,但通知工作并不顺利。

在模拟器(API29 和 Android 10)上,它可以正确发送它们,但是当我在真实设备上安装它时,它要么根本不工作,要么工作得很好。

直到我将手机更新到 android 12 之前,我的手机都收到了完美的通知,从那时起就没有任何通知被触发。我还在旧设备 (Android 7) 上测试了它,但它再次不起作用。

我读了它,但并不真正理解这些通道是如何工作的,所以我认为问题可能存在,但我发现它在某些设备/模拟器上仍然可以工作的方式很奇怪。

这是我的代码:

class MyReceiver: BroadcastReceiver() {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {

        val notificationChannel =
          NotificationChannel("My Channel", "New Quote", 
          NotificationManager.IMPORTANCE_DEFAULT).apply {
            description = "Alerts when A new daily quote is set!"
          }

        val titles = arrayOf(
          "Become inspired!",
          "Check out this quote!",
          "A new quote appeared!",
          "Daily quote available!"
        )
        val title = titles.random()

        val i = Intent(context, Qinperation::class.java)

        val builder = NotificationCompat.Builder(context, "My Channel")
          .setSmallIcon(R.drawable.ic_stat_name)
          .setContentTitle(title)
          .setContentText("A new daily quote is available for viewing")
          .setContentIntent(
            PendingIntent.getActivity(
              context,
              0,
              i,
              PendingIntent.FLAG_UPDATE_CURRENT
            )
          );

        with(NotificationManagerCompat.from(context)) {
          createNotificationChannel(notificationChannel)
          notify(1, builder.build())
        }
    }
}

感谢所有帮助:)

【问题讨论】:

    标签: android kotlin push-notification notification-channel


    【解决方案1】:

    Android 版本 O 及以上版本需要频道。您需要在收到通知之前创建频道。频道基本上就像一个“开关”,可以从应用程序的设置中关闭和打开,您可以配置通知,这意味着您可以配置是否收到通知是否应该振动等。

    在 android 版本 O 之前,您不需要通道,并且通知的配置是从 notificationBuilder 进行的。

    因此,如果您想为 O 版本以上和以下版本配置通知,则必须在两个地方都进行。

    现在关于你的问题,我认为,你创建延迟通道并在你的第一个活动或应用程序类中调用此方法:

    private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
       }
    }
    

    然后在你的接收器中你只需要创建你的通知并最终调用它:

    with(NotificationManagerCompat.from(this)) {
                // notificationId is a unique int for each notification that you must define
                notify(notificationId, builder.build())
            }
    

    如果这不起作用,您可能需要添加日志以查看是否确实调用了您的接收器。

    【讨论】:

      【解决方案2】:

      这是我的错。事实证明,在某些设备上(比如我正在测试的设备),不支持我用来触发通知的未决意图。如果人们有类似的问题,请尝试将 Pending Intents 更改为 - PendingIntent.FLAG_IMMUTABLE,因为这适用于更多设备,尤其是较新的设备!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 2018-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多