0

I have a local notification that should show daily, it works very good but after system rebooting it shows only once and then it never shows again, This is my code in the Activity

Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);
    if (Calendar.getInstance().after(cal)) {
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }
    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

And this is my Broadcast receiver:

 public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager;
    notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent mainIntent=new Intent(context, ReadingQuranActivity.class);
    mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mainIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);


    PendingIntent pendingIntent=PendingIntent.getActivity(context,100,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    android.support.v4.app.NotificationCompat.Builder builder=new android.support.v4.app.NotificationCompat.Builder(context).setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.read)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.read))
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setContentText(context.getResources().getString(R.string.notification_werd)).setAutoCancel(true);
    notificationManager.notify(100,builder.build());
}

And here is my permissions and intent filters in the Android Manifest :

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />    
<receiver
                android:name=".recivers.AlarmReceiver"
                android:enabled="true"
                android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter>
                    <action android:name="android.media.action.DISPLAY_NOTIFICATION" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
Randa Omar Fahmy
  • 303
  • 1
  • 3
  • 10
  • 1
    Alarms do not persist through a reboot. Upon rebooting, your alarm Receiver is getting the boot broadcast - since you've set it up to do so - and issuing a Notification then, but the alarm itself is not being set again. – Mike M. Apr 19 '17 at 01:49
  • Yeah make sense, so how can I solve this issue? Shall I check if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) then reset the alarm again? – Randa Omar Fahmy Apr 19 '17 at 02:12
  • 2
    Yeah, you've got the right idea, but when the alarm fires, `getAction()` will return null, 'cause you're not setting an action on that `Intent`. Flip that comparison around - `Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())` - or do a null check first. You could also use a separate Receiver, like you'd said. Whichever way you want to handle it. – Mike M. Apr 19 '17 at 02:17
  • 1
    Million thanks :)) – Randa Omar Fahmy Apr 19 '17 at 02:21
  • 2
    i fixed this issue after read this comments .. @MikeM. thanks a lot. – Rathiga Jesika Feb 03 '18 at 05:40

0 Answers0