IntentService pending intent using Alarm Manager not triggering? [duplicate] - android

I'm trying to set an alarm to fire every 5 minutes.
This is the code for setting the alarm :
#Override
public void scheduleAlarmManager() {
Timber.i("After SignIn sets AlarmManager");
// broadcast
Intent intent = new Intent(this, PatientAlarmReceiver.class);
intent.setAction(PATIENT_START_ALARM_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, 0);
// and set alarmManager
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar currentCal = Calendar.getInstance();
long currentTIme = currentCal.getTimeInMillis();
// if there's not an Alarm already set then set one
if (!isAlarmSet(this)) {
Timber.i("Alarm not set - so set one");
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent);
}
}
and I can verify that I set the alarm correctly since I see in my logcat the messages I log with Timber.
My Receiver class is :
public class PatientAlarmReceiver extends BroadcastReceiver {
public static final String TAG = "PATIENT-ALARM-RECEIVER";
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Inside OnReceive Patient");
Timber.i("Inside OnReceive Patient");
if (intent == null || intent.getAction() == null) {
return;
}
String action = intent.getAction();
if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) {
onStartCheckForConnectionRequest(context);
}
}
/**
* If is connected to network starts services
*/
private void onStartCheckForConnectionRequest(Context context) {
NetworkUtils networkUtils = new NetworkUtils(context);
if (networkUtils.isNetworkConnected()) {
Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class);
context.startService(checkForConnRequestIntent);
Timber.i("Starts Service From PatientALARMMANAGER");
}
}
}
And I have declared in Manifest :
<!-- Receivers -->
<receiver
android:name="bp.headsup.receivers.PatientAlarmReceiver" />
Also if I run : adb shell dumpsys alarm
I can see :
ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}
Mock in the above response is the sourceSet I'm using - dont know if it has anything to do with this I just mention it.
The problem is I never read in logcat the messages I have in onReceive on my Receiver class, and obviously no service starts. Anyone can help with that ? I'm using a device which runs with kitKat 4.4 (api 19) but I have tried it with an emulator too and the result was the same.

You're setting an ELAPSED_REALTIME alarm, which is based on the time since the last boot. However, you're passing it a starting time based on the "wall clock", so your alarm is actually set quite far in the future.
You can either change the alarm to an RTC type, or get the starting time from SystemClock.elapsedRealtime(). Given your described behavior, keeping the elapsed type and correcting the starting time seems appropriate.

Related

Xamarin AlarmManager not firing when app sleeps

I have following code to fire an alarm at exact time. When my app goes to sleep/background, Alarm does not FIRE, however as soon as I unlock my phone, then it fires right away.
Intent i = new Intent(this, typeof(MyReceiver));
i.PutExtra("Speaker", txtSpeaker.Text);
PendingIntent pi = PendingIntent.GetBroadcast(this, 0, i, 0);
string _date = DateTime.Today.ToString("MM-dd-yyyy") ;
string _time = tpick.Hour + ":" + tpick.Minute;
DateTime scheduleAt = Convert.ToDateTime(_date).Add(TimeSpan.Parse(_time));
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(scheduleAt.ToString());
var millisec = dateOffsetValue.ToUnixTimeMilliseconds();
AlarmManager alarmManager = (AlarmManager)GetSystemService(AlarmService);
alarmManager.SetExact(AlarmType.RtcWakeup, millisec, pi);
and here is my Receiver Class...
[BroadcastReceiver]
public class MyReceiver : BroadcastReceiver
{
public async override void OnReceive(Context context, Intent intent)
{
Console.WriteLine("FIRED");
String result = intent.Extras.GetString("Speaker");
Toast.MakeText(context, "Alarm Ringing!", ToastLength.Short).Show();
}
}
If the App is running on API 23 or newer, you might want to look into using the SetExactAndAllowWhileIdle method. According to the documentation it appears to me that this method will ignore whether the Device is in some low-power state or doze is enabled.

Alarm intent entering rapid infinite loop

I'm trying to code a block of code that has to wake up a few times a day and notify a server. I'm trying to use an alarm intent and a broadcast receiver but the receiver is being rapidly triggered infinitely and I can't seem to stop this.
All of my code sits in one file. The process flow is simple.
Wake up on boot, check if we should communicate, attempt to communicate else set up one of two waiting conditions, activate alarm.
Wake up on alarm, attempt to communicate, re-activate alarm if necessary, otherwise kill it.
When I build and deploy this apk on my device the following process flow happens:
reboot
receiver receives boot intent just fine
alarm gets scheduled
alarm intent gets triggered after 80 seconds as intended
then after the next 80 seconds,
then log-cat shows the broadcast receiver being triggered very rapidly. Several times a second as if its being spammed.
I am completely baffled at why its behaving like this
private PendingIntent pendingIntent;
/**
* Receive a signal, in our case, the device has booted up
* #param context The Context in which the receiver is running.
* #param intent The Intent being received.
*/
#SuppressLint("UnsafeProtectedBroadcastReceiver")
#Override
public void onReceive(Context context, Intent intent) {
Log.d("autostart", "broadcast received");
if(intent.getAction()==null)return;
Intent alarmIntent = new Intent(context, autostart.class);
alarmIntent.setAction("device.activation.alarm");
pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);//cancel flag seems to be ignored
cancel(context);//cancel command seems to be ignored
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
Log.d("autostart","We have booted");
RegisterActivation registerActivation = new RegisterActivation(context);
if(registerActivation.AlreadyActivated())
return;//no need
if(registerActivation.ActivationPending()){
//perform regular activation
return;
}
if(registerActivation.canComplete()){
boolean success = registerActivation.sendActivation();//talk to server
if(success) {
registerActivation.markCompleted();
cancel(context);
}
else {
registerActivation.markFileWaiting();
startPending(context);
}
return;
}
if(registerActivation.shouldWait()){//if can complete fails, then shouldWait will immediately return true
Log.d("autostart", "waiting");
registerActivation.markFileSimWait();
startWait(context);
return;
}
}
if(intent.getAction().equals("device.activation.alarm")){
Log.d("autostart","alarm triggered");
cancel(context);
RegisterActivation registerActivation = new RegisterActivation(context);
if(registerActivation.AlreadyActivated()){//for now always false
cancel(context);
return;
}
if(registerActivation.ActivationPending()){//for now always false
//same as before
return;
}
if(registerActivation.canComplete()){//glitch happens here
if(registerActivation.sendActivation()){
registerActivation.markCompleted();
cancel(context);
}else{
registerActivation.markFileWaiting();
startPending(context);//this immediatly triggers the broadcast recieve
}
return;
}
if(registerActivation.shouldWait()){
registerActivation.markFileSimWait();
startWait(context);
}
}
}
public void startPending(Context context) {
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 80000;//will later become 4 hours
manager.set(AlarmManager.RTC_WAKEUP,interval,pendingIntent);
Log.d("autostart", "alarm activated");
}
public void startWait(Context context) {//same function but different time interval
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 90000;// will later become 12 hours
manager.set(AlarmManager.RTC_WAKEUP, interval, pendingIntent);
}
public void cancel(Context context) {
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
}
thank you to Mike M.
https://stackoverflow.com/users/2850651/mike-m
I'd mark your comment as the answer but you only commented
I went from
manager.set(AlarmManager.RTC_WAKEUP,interval,pendingIntent);
to:
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+interval, interval, pendingIntent);
I had to add the interval to the intervalMillis paramater.
And as tested, the manager.set(...) function is really not responding all too well to what I want it to do so I just have to trigger a cancel at the start.
Thanks again Mike :)

Notification not being scheduled Android [duplicate]

I'm trying to set an alarm to fire every 5 minutes.
This is the code for setting the alarm :
#Override
public void scheduleAlarmManager() {
Timber.i("After SignIn sets AlarmManager");
// broadcast
Intent intent = new Intent(this, PatientAlarmReceiver.class);
intent.setAction(PATIENT_START_ALARM_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, 0);
// and set alarmManager
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar currentCal = Calendar.getInstance();
long currentTIme = currentCal.getTimeInMillis();
// if there's not an Alarm already set then set one
if (!isAlarmSet(this)) {
Timber.i("Alarm not set - so set one");
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent);
}
}
and I can verify that I set the alarm correctly since I see in my logcat the messages I log with Timber.
My Receiver class is :
public class PatientAlarmReceiver extends BroadcastReceiver {
public static final String TAG = "PATIENT-ALARM-RECEIVER";
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Inside OnReceive Patient");
Timber.i("Inside OnReceive Patient");
if (intent == null || intent.getAction() == null) {
return;
}
String action = intent.getAction();
if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) {
onStartCheckForConnectionRequest(context);
}
}
/**
* If is connected to network starts services
*/
private void onStartCheckForConnectionRequest(Context context) {
NetworkUtils networkUtils = new NetworkUtils(context);
if (networkUtils.isNetworkConnected()) {
Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class);
context.startService(checkForConnRequestIntent);
Timber.i("Starts Service From PatientALARMMANAGER");
}
}
}
And I have declared in Manifest :
<!-- Receivers -->
<receiver
android:name="bp.headsup.receivers.PatientAlarmReceiver" />
Also if I run : adb shell dumpsys alarm
I can see :
ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}
Mock in the above response is the sourceSet I'm using - dont know if it has anything to do with this I just mention it.
The problem is I never read in logcat the messages I have in onReceive on my Receiver class, and obviously no service starts. Anyone can help with that ? I'm using a device which runs with kitKat 4.4 (api 19) but I have tried it with an emulator too and the result was the same.
You're setting an ELAPSED_REALTIME alarm, which is based on the time since the last boot. However, you're passing it a starting time based on the "wall clock", so your alarm is actually set quite far in the future.
You can either change the alarm to an RTC type, or get the starting time from SystemClock.elapsedRealtime(). Given your described behavior, keeping the elapsed type and correcting the starting time seems appropriate.

Scheduled alarm never calls receiver class

I'm trying to set an alarm to fire every 5 minutes.
This is the code for setting the alarm :
#Override
public void scheduleAlarmManager() {
Timber.i("After SignIn sets AlarmManager");
// broadcast
Intent intent = new Intent(this, PatientAlarmReceiver.class);
intent.setAction(PATIENT_START_ALARM_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, 0);
// and set alarmManager
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar currentCal = Calendar.getInstance();
long currentTIme = currentCal.getTimeInMillis();
// if there's not an Alarm already set then set one
if (!isAlarmSet(this)) {
Timber.i("Alarm not set - so set one");
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent);
}
}
and I can verify that I set the alarm correctly since I see in my logcat the messages I log with Timber.
My Receiver class is :
public class PatientAlarmReceiver extends BroadcastReceiver {
public static final String TAG = "PATIENT-ALARM-RECEIVER";
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Inside OnReceive Patient");
Timber.i("Inside OnReceive Patient");
if (intent == null || intent.getAction() == null) {
return;
}
String action = intent.getAction();
if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) {
onStartCheckForConnectionRequest(context);
}
}
/**
* If is connected to network starts services
*/
private void onStartCheckForConnectionRequest(Context context) {
NetworkUtils networkUtils = new NetworkUtils(context);
if (networkUtils.isNetworkConnected()) {
Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class);
context.startService(checkForConnRequestIntent);
Timber.i("Starts Service From PatientALARMMANAGER");
}
}
}
And I have declared in Manifest :
<!-- Receivers -->
<receiver
android:name="bp.headsup.receivers.PatientAlarmReceiver" />
Also if I run : adb shell dumpsys alarm
I can see :
ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}
Mock in the above response is the sourceSet I'm using - dont know if it has anything to do with this I just mention it.
The problem is I never read in logcat the messages I have in onReceive on my Receiver class, and obviously no service starts. Anyone can help with that ? I'm using a device which runs with kitKat 4.4 (api 19) but I have tried it with an emulator too and the result was the same.
You're setting an ELAPSED_REALTIME alarm, which is based on the time since the last boot. However, you're passing it a starting time based on the "wall clock", so your alarm is actually set quite far in the future.
You can either change the alarm to an RTC type, or get the starting time from SystemClock.elapsedRealtime(). Given your described behavior, keeping the elapsed type and correcting the starting time seems appropriate.

Android - Trouble with service sending multiple local notifications

I've inherited a code base for an Android app and I'm facing a particularly though problem with local notifications.
The idea is to send a notification for each event which is scheduled in the future, considering also the reminder preference on how many minutes before the event the user wants to be notified.
Everything works just fine, except that after the notification is thrown for the first time, if the user opens the app before the event starts, the notification gets thrown another time. This happens every time the app is opened between (event start date - reminder) and event start date.
I've already gave a look at this and also this with no luck.
I've read that using a service may cause exactly this problem and some suggest to remove it but I think this is needed since the notification must be thrown also when the app is closed.
Currently the structure of the code is the following:
Edit - updated description of TabBarActivity
Inside TabBarActivity I have the method scheduleTravelNotification that schedules the AlarmManager.
This method is executed everytime there is a new event to be added on local database, or if an existing event have been updated.
The TabBarActivity runs this method inside the onCreate and onResume methods.
TabBarActivity is also the target of the notification - onclick event.
private static void scheduleTravelNotification(Context context, RouteItem routeItem) {
long currentTime = System.currentTimeMillis();
int alarmTimeBefore = routeItem.getAlarmTimeBefore();
long alarmTime = routeItem.getStartTime() - (alarmTimeBefore * 1000 * 60);
if(alarmTimeBefore < 0){
return;
}
if(alarmTime < currentTime){
return;
}
Intent actionOnClickIntent = new Intent(context, TravelNotificationReceiver.class);
PendingIntent travelServiceIntent = PendingIntent.getBroadcast(context, System.currentTimeMillis(), actionOnClickIntent, PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(alarmTime);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), travelServiceIntent);
Log.e("NEXT ALARM", "Time: " + String.valueOf(calendar.getTimeInMillis()));
}
This is TravelNotificationReceiver.java (should I use LocalBroadcastReceiver instead of BroadcastReceiver?)
public class TravelNotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("RECEIVER", "received TravelNotification request");
Intent notificationIntent = new Intent(context, TravelNotificationService.class);
context.startService(notificationIntent);
}
}
TravelNotificationService.java extends NotificationService.java setting as type = "Travel", flags = 0, title = "something" and text = "something else".
public abstract class NotificationService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
sendNotification();
return super.onStartCommand(intent, flags, startId);
}
public abstract String setNotificationType();
public abstract int setNotificationFlags();
public abstract String setNotificationTitle();
public abstract String setNotificationText();
/**
* Executes all the logic to init the service, prepare and send the notification
*/
private void sendNotification() {
int flags = setNotificationFlags();
String type = setNotificationType();
NotificationHelper.logger(type, "Received request");
// Setup notification manager, intent and pending intent
NotificationManager manager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intentAction = new Intent(this.getApplicationContext(), TabBarActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intentAction, flags);
// Prepares notification
String title = setNotificationTitle();
String text = setNotificationText();
Notification notification = NotificationHelper.buildNotification(getApplicationContext(), title, text, pendingIntent);
// Effectively send the notification
manager.notify(101, notification);
NotificationHelper.logger(type, "Notified");
}
}
Edit - Here's the code for NotificationHelper.buildNotification
public static Notification buildNotification(Context context, String title, String text, PendingIntent pendingIntent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
builder.setContentText(text);
builder.setContentTitle(title);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.mipmap.launcher);
builder.setCategory(Notification.CATEGORY_MESSAGE);
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
return builder.build();
}
Thank you for the answers!
Edit I've seen also this but has no accepted answers, while this post suggest something that I think it's already managed with if(alarmTime < currentTime){ return; } in scheduleTravelNotification.
This may not be your exact problem, but at a glance, you're sending the notification in onStartCommand() which can itself be run many times during the lifetime of the service -- for example, if you issue the service start command "blindly" in an onCreate of an activity, it will happen every time the activity is (re)created.
You have a few options for handling this.
One is to create a boolean flag as a property of the service, default to false, and check it before sending the notification. If it's false, send the notification and set it to true, and if it's already true you do not send a notification.
Another is to check and see if the service is already running, and if it is, don't send the service start command in the first place. This can be tedious to do everywhere, and violates DRY, so if you take this route you may want to create a static method in your service class which checks to see if the service is running and then starts it if not, and call that instead of explicitly starting the service.
Similar to user3137702 answer you could simple have a static boolean of APPISINFORGROUND which is checked everytime the send notification method is hit, and managed from your application/activities code.
As User said it is likely that your onStartCommand method is being called at odd times due to the app / service lifecycle.
Alternatively check your receiver is not being called somewhere else from your code.
It may be your NotificationHelper class which is causing an issue. Please share the code for this class.
One thought may be that your notification is not set to be auto cancelled, check if you include the setAutoCancel() method in your Notification Builder.
Notification notification = new Notification.Builder(this).setAutoCancel(true).build();
I've found a way to make it work, I'm posting this since it seems to be a problem of many people using the approach suggested in this and this articles. After months of testing I can say I'm pretty satisfied with the solution I've found.
The key is to avoid usage of Services and rely on AlarmScheduler and Receivers.
1) Register the receiver in your manifest by adding this line:
<receiver android:name="<your path to>.AlarmReceiver" />
2) In your activity or logic at some point you want to schedule a notification related to an object
private void scheduleNotification(MyObject myObject) {
// Cal object to fix notification time
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(myObject.getTime());
// Build intent and extras: pass id in case you need extra details in notification text
// AlarmReceiver.class will receive the pending intent at specified time and handle in proper way
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("OBJECT_ID", myObject.getId());
// Schedule alarm
// Get alarmManager system service
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Build pending intent (will trigger the alarm) passing the object id (must be int), and use PendingIntent.FLAG_UPDATE_CURRENT to replace existing intents with same id
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), myObject.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Finally schedule the alarm
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
3) Define AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Find object details by using objectId form intent extras (I use Realm but it can be your SQL db)
MyObject myObject = RealmManager.MyObjectDealer.getObjectById(intent.getStringExtra("OBJECT_ID"), context);
// Prepare notification title and text
String title = myObject.getSubject();
String text = myObject.getFullContent();
// Prepare notification intent
// HomeActivity is the class that will be opened when user clicks on notification
Intent intentAction = new Intent(context, HomeActivity.class);
// Same procedure for pendingNotification as in method of step2
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, myObject.getId(), intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
// Send notification (I have a static method in NotificationHelper)
NotificationHelper.createAndSendNotification(context, title, text, pendingNotificationIntent);
}
}
4) Define NotificationHelper
public class NotificationHelper {
public static void createAndSendNotification(Context context, String title, String text, PendingIntent pendingNotificationIntent) {
// Get notification system service
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Build notification defining each property like sound, icon and so on
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(text);
notificationBuilder.setSmallIcon(R.drawable.ic_done);
notificationBuilder.setCategory(Notification.CATEGORY_MESSAGE);
notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingNotificationIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
notificationManager.notify(1001, notificationBuilder.build());
}
}
At this point it should work and schedule / trigger notification at the right time, and when notification is opened it will appear only once starting the activity declared in notification pending intent.
There is still a problem, AlarmManager have a "volatile" storage on user device, so if user reboots or switch off the phone you will lose all intents that you previously scheduled.
But fortunately there is also a solution for that:
5) Add at top of your manifest this uses permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
6) Right below the line added at step 1 register the boot receiver
<receiver android:name="<your path to>.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
7) Define the BootReceiver
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do something very similar to AlarmReceiver but this time (at least in my case) since you have no source of intents loop through collection of items to understand if you need to schedule an alarm or not
// The code is pretty similar to step 3 but repeated in a loop
}
}
At this point your app should be able to schedule / trigger notification and restores those reminders even if the phone is switched off or rebooted.
Hope this solution will help someone!

Categories

Resources