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

AlarmManager+NotificationManager无法正常工作问题求助

问题排查与修复方案

我帮你梳理几个高频的问题点,这些都是定时通知不生效的常见原因,照着调整应该就能解决:

1. 别忘注册广播接收器

你需要在AndroidManifest.xml里注册Notification_receiver,不然系统找不到这个广播接收器,自然没法触发通知:

<receiver android:name=".Notification_receiver"
          android:exported="false">
    <intent-filter>
        <action android:name="MY_NOTIFICATION_MESSAGE" />
    </intent-filter>
</receiver>

2. Android 8.0+必须创建通知渠道

Android 8.0(API 26)及以上版本,没有绑定渠道的通知是不会显示的,你的代码完全没处理这一步。修改Notification_receiver的onReceive方法,先补全渠道创建逻辑:

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    // 先创建通知渠道(Android 8.0+ 必填)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "DAILY_NOTIFY_CHANNEL";
        String channelName = "每日提醒";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }
    
    Intent repeating_intent = new Intent(context,Chapterchoice.class);
    String daily10 = "daily";
    intent.putExtra("daily",daily10);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sample Title")
            .setContentText("same message")
            .setAutoCancel(true);
    
    // 给builder绑定渠道ID(Android 8.0+ 必填)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId("DAILY_NOTIFY_CHANNEL");
    }
    
    if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
        notificationManager.notify(100, builder.build());
    }
}

3. 适配Android 12+的PendingIntent Flag

Android 12(API 31)及以上版本,创建PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,否则会触发异常(你说代码没报错,可能是测试设备版本较低,但最好提前适配)。修改MainActivity里的PendingIntent创建代码:

var pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
} else {
    PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT)
}

4. 处理时间逻辑:避免错过触发时机

如果当前时间已经过了你设置的21:53,alarmManager.setRepeating会等到第二天的同一时间才触发。要是想快速测试,可以把时间改成当前时间之后的几分钟,或者加个判断自动顺延一天:

val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY,21)
calendar.set(Calendar.MINUTE,53)
calendar.set(Calendar.SECOND,0)

// 如果当前时间已过设定时间,自动加一天
if (calendar.timeInMillis < System.currentTimeMillis()) {
    calendar.add(Calendar.DAY_OF_YEAR, 1)
}

5. Android 13+要申请通知权限

Android 13(API 33)及以上版本,需要动态申请POST_NOTIFICATIONS权限才能弹出通知。在MainActivity里补全权限申请逻辑:

// 在onCreate方法里检查权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 101)
    }
}

同时在AndroidManifest.xml里添加权限声明:

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

测试小建议

  • 先把触发时间设为当前时间之后1分钟,快速验证逻辑是否生效
  • 检查应用的通知开关是否在系统设置里打开了
  • 确保R.drawable.ic_launcher这个图标存在,不然通知可能因为图标问题无法显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 07:17:55