首页 > 解决方案 > 设置重复通知android kotlin

问题描述

在过去的 4 天里,我一直在尝试设置正常工作的重复通知,老实说,我不知道为什么现在这不起作用:

(NotificationCreator().createRepeatingNotification() 从 mainActivity 被调用(并且数据被传递):

class NotificationCreator {
    fun createRepeatingNotification(context: Context, day: Int, hour: Int) {
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager
        val intent = Intent(context, NotificationReceiver::class.java)
        val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val currentDate = Calendar.getInstance()
        currentDate.set(Calendar.DAY_OF_WEEK, day)
        currentDate.set(Calendar.HOUR_OF_DAY, hour);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);

        alarmManager!!.setRepeating(
            AlarmManager.RTC_WAKEUP,
            currentDate.timeInMillis,
            AlarmManager.INTERVAL_DAY * 7,
            pendingIntent
        )
        Log.i("createRepeatingNotification", "created new notification")
    }


    // this gets executed inside function "onCreate" in MainActivity
    fun createNotificationChannel(context: Context){
        val descriptionText = "idk"
        val importance = NotificationManager.IMPORTANCE_HIGH
        val channel = NotificationChannel(Constants.channelId, Constants.channelName, importance).apply {
            description = descriptionText
            enableLights(true)
            enableVibration(true)
            lightColor = Color.parseColor("#84ff00")
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            vibrationPattern = longArrayOf(0, 500, 100, 500, 1000)
        }
        val notificationManager: NotificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.createNotificationChannel(channel)
    }

}

class NotificationReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val _intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, _intent, 0)

        val builder = NotificationCompat.Builder(context, Constants.channelName)
            .setSmallIcon(context.applicationInfo.icon)
            .setContentTitle(context.applicationInfo.name)
            .setContentText("Your plants need some attention")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setSound(soundUri)
            .setLights(Color.parseColor("#84ff00"), 1100, 500)
            .setVibrate(longArrayOf(0, 500, 100, 500, 1000))
            .setAutoCancel(true)

        with(NotificationManagerCompat.from(context)) {
            notify(UtilRandomNumber.randomNumber, builder.build())
        }
    }
}

我已经在 AndroidManifest 中声明了使用的权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />

我错过了什么/做错了什么吗?

所有帮助将不胜感激,谢谢:)

编辑:

我也声明了接收者

        <receiver android:name=".notifications.NotificationReceiver" android:enabled="true"/>

标签: androidkotlinnotificationsalarmmanagerschedule

解决方案


推荐阅读