首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不同的时间设置多个带有AlarmManager的通知?

如何在不同的时间设置多个带有AlarmManager的通知?
EN

Stack Overflow用户
提问于 2022-01-07 13:37:15
回答 1查看 45关注 0票数 0

这是我的问题:我有通知列表,希望在不同的时间显示它们,但它们同时出现在一起。

首先调用updateLabel函数:

私有空updateLabel(ArrayList notificationEntities){

代码语言:javascript
运行
复制
    int timer = 5000;

    for (int i = 0; i < notificationEntities.size(); i++) {

        Notification notification = addNotification(notificationEntities.get(i));
        scheduleNotification(notificationEntities.get(i).getId(), notification, (long) timer);

        timer += 10000;
    }

}

然后创建通知:

私人通知addNotification(NotificationEntity notificationEntity) {

代码语言:javascript
运行
复制
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, default_notification_channel_id)
            .setSmallIcon(R.drawable.ic_baseline_android_24)
            .setContentTitle("Notifications Example")
            .setContentText("This is a test notification for " + notificationEntity.getType() + " with id " + notificationEntity.getId())
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setVibrate(new long[]{0, 1000, 1000})
            .setChannelId(NOTIFICATION_CHANNEL_ID);

    return notification.build();
}

然后是scheduleNotification:

私有无效scheduleNotification(int notificationId,通知通知,长延迟){

代码语言:javascript
运行
复制
    int requestID = (int) System.currentTimeMillis();

    Intent notificationIntent = new Intent(this, NotificationPublisher.class);

    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);

    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(this, requestID, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delay, pendingIntent);

}

这是我的NotificationPublisher课程:

公共类NotificationPublisher扩展BroadcastReceiver {

代码语言:javascript
运行
复制
public static final String NOTIFICATION = "myNotification";
public static final String NOTIFICATION_ID = "myNotification-id";
@Override
public void onReceive(Context context, Intent intent) {


    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

        NotificationChannel channel = new NotificationChannel(ClientHomepage.NOTIFICATION_CHANNEL_ID, "Channel", NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("This is just notification");
        notificationManager.createNotificationChannel(channel);

    }
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);
}

}

我的问题是所有的通知都是同时发出的,而不是一个接一个的。我尝试过更改requestID,但是它没有帮助,我也尝试过更改PendingIntent标志,这也没有帮助。希望你有什么建议。

EN

Stack Overflow用户

发布于 2022-01-07 14:36:09

根据我的理解,您在这一行代码中添加了错误的时间。

scheduleNotification(notificationEntities.get(i).getId(),通知,(长)定时器);

而不是(long)timer,您必须添加System.currentTimeinMillis()+timer,它将解决您的问题

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70622061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档