Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintentandroid-alarms

BroadcastReceiver stops receiving after few days (Local Notification)


I'm using a BroadCastReceiver for triggering alarm which is working fine for like 2-3 days, after that it stops working, I am not receiving any alerts in my BroadcastReceiver. Is there a lifeSpan for Pending Intent? Actually I'm creating alarms for 30 days. I'm using PendingIntents. Here is the code for PendingIntent:

PendingIntent pendingIntentScheduler = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);            
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntentScheduler);

and the receiver at BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
     this.context = context;        
     Log.d("onReceive", "this is broadcast reciever");        
}

Android Manifest Permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
  <application>
  <receiver android:name=".medicine.modal.AlarmReceiver" android:permission="android.permission.WAKE_LOCK"
            android:enabled="true" android:exported="true" android:process=":remote">
            <intent-filter>
                <action android:name="com.healthsaverz.nimap.healthmobile.healthsaverz.mainscreen.controller.NotificationActivity" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
   </application>

Solution

  • I found a solution, not legitimate but a bypass. May be this will help someone. What I did is put a repeating Alarm which repeats at every 24 hours, in which I'm recreating all the Pending Intents. Since the life of pending intent is 2-3 days (According to what i found in my app). This results in creating new Pending Intents everyday.