简体   繁体   English

android-如何在特定时间推送通知(提醒)?

[英]android - how to push notification (reminder) on a specific time?

I checked almost all questions in stack overflow and also in google documentation and still couldn't understand what to use exactly for my cases. 我检查了堆栈溢出和Google文档中的几乎所有问题,但仍然不明白该如何准确地用于我的案例。

What i want is, user will select reminder date in the app and in that time app will send a notification even app is closed or phone restarted between remind set time and remind time. 我想要的是,用户将在应用程序中选择提醒日期,并且在该时间内应用程序将发送通知,即使应用程序在提醒设置时间和提醒时间之间关闭或手机重新启动也是如此。

So what do i need to use? 那我需要用什么呢? Which classes do i need? 我需要哪些课程? Broadcast Receiver, AlarmManager these two is enough or what? 广播接收器,AlarmManager这两个足够还是什么? a sample of code 20-30 lines would be nice =) 代码20-30行的示例将很不错=)

You will need both a few things. 您将同时需要两件事。

Setup: You need to set your app to listen for the phone starting by registering for this intent in your manifest. 设置:您需要先在清单中注册意图,然后才能将您的应用设置为侦听电话。

Basically you when your user selects the time, you will need to: 基本上,当用户选择时间时,您将需要:

  1. Save that time in a file/db/shared prefs. 将该时间保存在文件/ db /共享的首选项中。
  2. Set the alarm manager to wake up your app at the right time 设置警报管理器以在适当的时间唤醒您的应用
  3. When the alarm manager sends you a broadcast, start a service to do the necessary work (broadcast receivers have to complete their work in a short amount of time, so it's better to start a service) 当警报管理器向您发送广播时,启动一项服务以执行必要的工作(广播接收者必须在短时间内完成其工作,因此最好启动一项服务)
  4. In your service, remove the time from your file/db/shared prefs 在您的服务中,从文件/数据库/共享首选项中删除时间

If your app ever gets the broadcast that the phone was rebooted, then all of your alarms are lost. 如果您的应用曾经广播过电话已重启的广播,则所有警报都将丢失。 You need to reset them using the times you saved in the file/db/shared prefs. 您需要使用保存在文件/ db /共享首选项中的时间来重置它们。

Check Android Developers website for calender intents so you can use them to determine the time etc. On your broadcast receiver pass the details or timing to the broadcast receiver. 请查看Android Developers网站上的日历意图,以便您可以使用它们来确定时间等。在广播接收器上,将详细信息或时间安排传递给广播接收器。 Once you do that use the alarm manager etc to check if the time is perfect and simply send a push notification when the time is correct. 一旦执行了此操作,请使用警报管理器等检查时间是否正确,并在时间正确时发送推送通知。

I say this assuming that you know already about the calendar, alarm manager, and broadcast receivers intents. 我说这是假设您已经了解日历,警报管理器和广播接收器的意图。 If you don't I recommend googling the following with Android Developers appended to it. 如果您不这样做,建议您将以下内容与附加的Android开发人员一起使用。

  1. Use AlertManager.set to send notification on particular time. 使用AlertManager.set在特定时间发送通知。
  2. For re-setting Alarm Notification, use 要重新设置警报通知,请使用

     <receiver android:name=".AfterBootReceiver" android:label="AfterBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> 

and

@Override public void onReceive(Context context,Intent intent){

    ...
    }

to reset AlarmManager.set after device re-booted. 重启设备后重置AlarmManager.set。

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

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