How to start notification in combination with AlarmManager?

I am trying to figure out how I should trigger a notification. Creating a notification is not what I ask, but rather a way to run it in the background so that its unobtrusiveness and user can do everything they did. This is for a calendar, a reminder to be exact. It is also important to note that I use AlarmManager .

  • Which method should be used to run in the background. BroadCastReciever , Service , etc.

  • The research I found also poses a problem with AlarmManager . When the application is killed or the phone is turned off, an alarm also appears. What other method should I use to ensure that a notification is guaranteed to remind this event?

If any additional information is needed, ask and I will do it. Thanks in advance.

+4
source share
3 answers

Create a broadcast broadcast or intenservice. Then...

 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Date date = new Date(); //set this to some specific time or Calendar calendar = Calendar.getInstance(); //set either of these to the correct date and time. then Intent intent = new Intent(); //set this to intent to your IntentService or BroadcastReceiver //then... PendingIntent alarmSender = PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); //or use PendingIntent.getBroadcast if you're gonna use a broadcast alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender); // date.getTime to get millis if using Date directly. 

If you want these alarms to work correctly even when the phone is rebooted, add:

  <action android:name="android.intent.action.BOOT_COMPLETED"/> 

as the testator at your receiver in the manifest and recreate your alarms in onReceive.

EDIT

When you create a BroadcastReceiver in your application, it allows you to do exactly what it sounds like: receive broadcasts in the system. So, for example, you can use BroadcastReceiver like this:

 public class MyAwesomeBroadcastReceiver extends BroadcastReceiver { //since BroadcastReceiver is an abstract class, you must override the following: public void onReceive(Context context, Intent intent) { //this method gets called when this class receives a broadcast } } 

To send translations to this class explicitly, you define the receiver inside the manifest as follows:

 <receiver android:name="com.foo.bar.MyAwesomeBroadcastReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="SOME_AWESOME_TRIGGER_WORD"/> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> 

If you have this in the manifest, you’ll get two things: you can send the broadcast explicitly to the recipient whenever you want,

 Intent i = new Intent("SOME_AWESOME_TRIGGER_WORD"); sendBroadcast(intent); 

In addition, since you told the android that you want to receive the BOOT_COMPLETED action, which is broadcast by the system, your receiver will also be called when this happens.

+2
source

Using AlarmManager is best practice.

+2
source

Here is what you can do:

  • Start the Service using AlarmManager's pending intent and write the Notification code on that service.

  • Use the database to store all of your Alarms and then transfer them to restart your device using the BOOT_COMPLETED Broadcast reciver .

+1
source

All Articles