在特定时间发送通知 [英] Send Notification at a specific time

查看:60
本文介绍了在特定时间发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我需要在下午 1 点发送通知.我怎样才能做到这一点并定期重复.<​​/p>

我需要使用重复代码"来实现此代码:

Intent intent = new Intent();PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);通知通知 = new NotificationCompat.Builder(this).setTicker("代码标题").setSmallIcon("图标").setContentTitle("通知内容标题").setContentText("输出").setContentIntent(pi).setAutoCancel(true).建造();NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationManager.notify(0, 通知);

解决方案

使用警报管理器类在您想要的时间开始意图.

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);日历日历 = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());日历.设置(日历.HOUR_OF_DAY,02);日历.设置(日历.MINUTE,00);//setRepeating() 让你指定一个精确的自定义间隔——在这种情况下,//1天alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis()/1000,AlarmManager.INTERVAL_DAY, alarmIntent);

在这里,您需要提供您想要调用意图的时间.在我的代码中,它是每晚凌晨 2 点.此外,对于重复间隔,我选择了一天,因为我的流程需要每天调用一次.

I need to, for example, at 1PM to send a notification. how can i do this and repeat it periodically.

I need this code to be implemented with the "repeat code" :

Intent intent = new Intent();

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);

    Notification notification = new NotificationCompat.Builder(this)
            .setTicker("Ticker Title")
            .setSmallIcon("icon")
            .setContentTitle("Notification Content Title")
            .setContentText("Output")
            .setContentIntent(pi)
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);

解决方案

Use alarm manager class to start an intent on your desired time.

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 02);
calendar.set(Calendar.MINUTE, 00);

// setRepeating() lets you specify a precise custom interval--in this case,
// 1 day
alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis()/1000,
        AlarmManager.INTERVAL_DAY, alarmIntent);

Here, you need to provide the time at which you want to call the intent. In my code, it is 2AM every night. Also, for repeat interval, I selected a day because my process needed to be called on daily basis.

这篇关于在特定时间发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆