You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Studio中如何设置通知发送的延迟时间?

实现延迟发送Android通知的解决方案

没问题,我来帮你搞定延迟发送通知的需求!你已经配置好了通知渠道,接下来只需要配合AlarmManagerPendingIntentBroadcastReceiver就能实现延迟触发的效果,下面是详细的步骤和代码修改:

第一步:创建一个广播接收器(BroadcastReceiver)

我们需要一个接收器来接收AlarmManager的延迟触发信号,然后在这个接收器里发送通知:

public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 初始化通知管理器
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        
        // 构建并发送延迟后的通知
        Notification notification = new NotificationCompat.Builder(context, MyNotificationPublisher.CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Hi")
                .setContentText("延迟20秒的测试通知")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();
        
        // 用不同的通知ID,避免覆盖之前的即时通知
        notificationManager.notify(2, notification);
    }
}

第二步:在Manifest中注册接收器

别忘了在AndroidManifest.xml里注册这个接收器,否则系统无法找到它:

<receiver android:name=".NotificationReceiver" />

第三步:修改发送通知的方法,设置延迟触发

把你EmailActivity里的sendOnChannel1方法改成下面这样,用AlarmManager设置20秒的延迟:

public void sendOnChannel1(View v) {
    // 设置延迟时间:20秒(单位:毫秒)
    long delayMillis = 20000L;
    // 计算触发时间:当前系统时间 + 延迟时间
    long triggerTime = System.currentTimeMillis() + delayMillis;

    // 创建Intent,指向我们刚才写的NotificationReceiver
    Intent intent = new Intent(this, NotificationReceiver.class);
    // 创建PendingIntent,用来让AlarmManager触发时调用接收器
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this,
            0, // 请求码,不同的延迟通知可以用不同的码区分
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE // 适配Android 12+
    );

    // 获取AlarmManager实例
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // 根据系统版本选择合适的闹钟设置方式
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Android 6.0及以上,使用精确且允许空闲时触发的闹钟(绕过省电限制)
        alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.RTC_WAKEUP, // 使用系统时间,设备休眠时唤醒
                triggerTime,
                pendingIntent
        );
    } else {
        // 旧版本系统使用精确闹钟
        alarmManager.setExact(
                AlarmManager.RTC_WAKEUP,
                triggerTime,
                pendingIntent
        );
    }

    // 给用户一个提示,告知通知将延迟发送
    Toast.makeText(this, "通知将在20秒后发送", Toast.LENGTH_SHORT).show();
}

关键点说明

  • AlarmManager.RTC_WAKEUP:使用系统时间来计算触发点,并且如果设备处于休眠状态,会唤醒设备来执行通知发送,保证通知能按时送达。
  • PendingIntent.FLAG_IMMUTABLE:Android 12及以上版本要求必须设置这个标记(或者FLAG_MUTABLE),这里我们不需要修改Intent内容,所以用IMMUTABLE更安全。
  • 通知ID:我用了2作为延迟通知的ID,和你原来的1区分开,避免覆盖之前的即时通知,如果不需要区分也可以用同一个ID。

额外小技巧

如果需要取消还未触发的延迟通知,可以调用alarmManager.cancel(pendingIntent),前提是创建PendingIntent的参数和设置时完全一致哦。

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

火山引擎 最新活动