首页 > 解决方案 > Android 提醒通知不起作用

问题描述

尝试搜索但没有运气

我正在测试 API 24

这段代码有什么问题?尽管我将优先级指定为高并正确添加了振动和声音,但通知显示正确,但不是提醒通知。

请任何帮助将不胜感激

这是我的代码:

private fun sendNotification(title: String, message: String) {
        val mNotificationCompatBuilder = mNotificationUtils.getNotificationBuilder(
            title,
            message,
            false,
            R.drawable.ic_logo,
            NotificationUtils.ALERT_MESSAGES_ID
        )

        mNotificationCompatBuilder.setStyle(NotificationCompat.BigTextStyle().bigText(message))

        mNotificationUtils.openTopActivityOnClick(mNotificationCompatBuilder, FTApplication.applicationContext())
        mNotificationUtils.setSoundAndVibrate(mNotificationCompatBuilder)

        val mNotificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        mNotificationManager.notify((title+message).hashCode(), mNotificationCompatBuilder.build())
    }


 fun getNotificationBuilder(title: String, body: String, onGoing: Boolean, icon: Int, id: String): NotificationCompat.Builder {
        val largeIcon = BitmapFactory.decodeResource(
            context.resources,
            R.drawable.ic_logo
        )

        return NotificationCompat.Builder(context, id)
            .setSmallIcon(icon)
            .setLargeIcon(largeIcon)
            .setBadgeIconType(icon)
            .setContentTitle(title)
            .setContentText(body)
            .setOngoing(onGoing)
            .setAutoCancel(!onGoing)
            .setWhen(System.currentTimeMillis())
    }


fun setSoundAndVibrate(builder: NotificationCompat.Builder) {

        @Suppress("DEPRECATION")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) builder.priority = NotificationManager.IMPORTANCE_HIGH

        else  builder.priority = Notification.PRIORITY_HIGH

        builder.priority = NotificationCompat.PRIORITY_HIGH   // heads-up test


        val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        builder.setSound(alarmSound)
        builder.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))

        builder.setDefaults(Notification.DEFAULT_ALL)
    }

这是我正在创建的频道(因为如上所述我现在正在 API 24 上测试,所以没有使用它)

@RequiresApi(Build.VERSION_CODES.O)
     private fun createAlertMessageChannel() {

         if (getManager()!!.getNotificationChannel(ALERT_CHANNEL_NAME) != null) {
             return
         }

         // create alert channel
         val alertChannel = NotificationChannel(
             ALERT_MESSAGES_ID,
             ALERT_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
         )

         alertChannel.description = "For Alerting User of Events"

         alertChannel.setShowBadge(false)

         alertChannel.enableLights(true)

         alertChannel.enableVibration(true)
         //alertChannel.setSound(null, null)
         // Sets the notification light color for notifications posted to this channel
         alertChannel.lightColor = Color.GREEN
         // Sets whether notifications posted to this channel appear on the lock screen or not
         alertChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

         getManager()!!.createNotificationChannel(alertChannel)
     }

标签: android

解决方案


推荐阅读