You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AlarmManager+Broadcast调度24小时后通知不触发问题求助

问题:长期调度的电影提醒通知无法触发,短时间内调度正常

我正在开发一款展示即将上映电影的APP,支持用户为特定电影创建提醒。用户点击提醒按钮后,APP需要一次性调度3条通知:

  • 电影上映前6天发送通知;
  • 电影上映前2天发送通知;
  • 电影上映当天发送通知。

我用AlarmManager + Broadcast实现,但上述通知都收不到,不过调度几小时后的通知能正常发送。推测问题出在次日及以后的通知调度上。

我在StackOverflow看到过相关讨论,说设备可能会清理已调度的闹钟(和电池优化有关),但没在Android官方文档里找到对应的内容。也试过用WorkManager,结果和AlarmManager一样有问题。另外我觉得AlarmManager更适合这类本地通知调度场景。

以下是我用到的代码:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.cinema">

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

    <application
        android:name=".application.CinemaApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Cinema"
        tools:targetApi="31">

        <receiver android:name="com.example.common.broadcast.MovieReminderBroadcast"/>

        <activity
            android:name=".MainAppActivity"
            android:exported="true">
            ...
        </activity>

    </application>

</manifest>

CinemaReminderManagerImpl.kt

val alarmManager = context.getSystemService(AlarmManager::class.java)

val intent = Intent(context, MovieReminderBroadcast::class.java).apply {
    putExtra("deeplink", deeplink)
    putExtra("message", "message")
    putExtra("notificationId", movieId)
}

val pendingIntent = PendingIntent.getBroadcast(
    context,
    movieId,
    intent,
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT } else { PendingIntent.FLAG_UPDATE_CURRENT }
)

alarmManager.set(
    AlarmManager.RTC_WAKEUP,
    timeInMillis,
    pendingIntent
)

MovieReminderBroadcast.kt

class MovieReminderBroadcast : BroadcastReceiver() {

    companion object {
        private const val CHANNEL_ID = "ReminderNotificationChannel"
    }

    override fun onReceive(context: Context?, receiverIntent: Intent?) {
        if (context == null) return

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                CHANNEL_ID,
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = "Reminder Channel Description"
            }
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }

        val deeplink = receiverIntent?.extras?.getString("deeplink").orEmpty()
        val message = receiverIntent?.extras?.getString("message").orEmpty()
        val notificationId = receiverIntent?.extras?.getInt("notificationId") ?: 0

        val intent = Intent().apply {
            action = Intent.ACTION_VIEW
            data = Uri.parse(deeplink)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntent = PendingIntent.getActivity(
            context,
            notificationId,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(
                context.resources.getString(R.string.common_title_notification)
            )
            .setContentText(
                message
            )
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .build()

        NotificationManagerCompat.from(context).notify(notificationId, notification)
    }
}

内容的提问来源于stack exchange,提问作者r96

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 11:47:37