简体   繁体   English

离线推送通知-提醒功能

[英]Offline push notification - reminder feature

I'm interested in adding notifications to my application, in the form of reminders.我有兴趣以提醒的形式向我的应用程序添加通知。

I've looked into notifications, but I have yet to find a way to do it without the app being open.我已经研究了通知,但我还没有找到一种方法来在不打开应用程序的情况下做到这一点。

For example, I want to add a time setting, that will store the time in day to send the notification.例如,我想添加一个时间设置,它将存储发送通知的时间。

Then, each day (or w\\e), at the given time, I want the user to receive a notification, which he can click to enter the application.然后,每天(或 w\\e),在给定的时间,我希望用户收到通知,他可以单击该通知进入应用程序。

How is it done?它是如何完成的?

You need to write a AlarmManager based program to schedule your application's calls and then send Local Notifications to the UI with NotificationCompat.Builder您需要编写一个基于AlarmManager的程序来安排应用程序的调用,然后使用NotificationCompat.Builder将本地通知发送到 UI
Have a look at the link below, it will get you started easily看看下面的链接,它会让你轻松入门
http://developer.android.com/training/scheduling/alarms.htmlhttp://developer.android.com/training/scheduling/alarms.html

Even better, you may want to look into GCMNetworkManager which is another great way for scheduling tasks to happen at a specific time.更好的是,您可能想要查看GCMNetworkManager ,这是在特定时间安排任务发生的另一种好方法。

You shouldn't need the App to be running for either Alarms, or other scheduled events to occur.您不需要为警报或其他计划事件的发生而运行应用程序。 There are few different options for generating timed events to trigger the notification.生成定时事件以触发通知的选项很少。

Here is an example, you would set the PendingIntent to start you notification:这是一个示例,您将设置 PendingIntent 以启动通知:

    private void setAlarm() {
        final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        final Intent i = new Intent(this, MyNotifService.class);
        final PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.cancel(pi);

        final Calendar cal = Calendar.getInstance();
        final String notifTime = prefs.getString("notif_time", "07:30");
        final String[] times = notifTime.split(":");
        final int hour = Integer.parseInt(times[0]);
        final int min = Integer.parseInt(times[1]);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);

        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pi);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM