0

I have been trying to build an app which checks for notification files every hour on a server. I have successfully implemented this using the alarm manager class, but the problem I am facing now is that the alarm does not get triggered on reboot, If the user does not launch the app after reboot. I want to automate this triggering of alarm. After searching for quite some time, I figured to launch an activity on reboot, which I know is bad practice, but is probably my only way out. I would like to check if there is some other way I could achieve what I want. I am open to not using alarm manager class for notifications. Please help with any suggestions.

This is my NotificationBootReceiver Class

package com.example.quickstart;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;

public class NotificationBootReceiver extends BroadcastReceiver {
    String txt = "";
    @Override
    public void onReceive(Context context, Intent intent) {
            Log.i("Message1234","Boot Successfull ");

            // Construct an intent that will execute the AlarmReceiver
            Intent i = new Intent(context, MyAlarmReceiver.class);
            // Create a PendingIntent to be triggered when the alarm goes off
            final PendingIntent pIntent = PendingIntent.getBroadcast(context, MyAlarmReceiver.REQUEST_CODE,
                    i, PendingIntent.FLAG_UPDATE_CURRENT);
            // Setup periodic alarm every every half hour from this point onwards
            long firstMillis = System.currentTimeMillis(); // alarm is set right away
            AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
            // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    2*60*60,pIntent);
            // alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
            //    AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);



    }
}

This is my android Manifest file receiver for NotificationBootReceiver.

  <receiver android:name=".NotificationBootReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
Kushagr Tyagi
  • 101
  • 2
  • 12

1 Answers1

0

Create a boot receiver in your app BootBroadcastReceiver:

public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context pContext, Intent intent) {
         // Start Alarm again...
    }
}

Then register this Receiver in your manifest.xml

<receiver
android:name="com.xyz.BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Add permission in AndroidManifest.xml :

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Suggestion:

By the way, I implemented repetitive notification by Alarm Manager old days. That has many issue when device is in Doze mode.

I would suggest you using Work Manager of Google Architecture component, which will work perfectly with pre 21 or post 21 versions of Android.

Update Also check right way to set Alarms in this answer

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • This is what I have done, but the problem with this is that this requires the user to launch the app at least once, in order to trigger the alarm. I want the alarm to be triggered automatically, without the user launching the application. – Kushagr Tyagi Jul 12 '18 at 05:16
  • No, boot receiver does not need to launch manually, perhaps you have some issue in your Alarm Manager. Try setting some Log in your boot receiver class to find issue. – Khemraj Sharma Jul 12 '18 at 05:18
  • Also check suggestion part. Which i have ended up after fighting with Alarm Manger. – Khemraj Sharma Jul 12 '18 at 05:19
  • Please look at this post, and the answer. – Kushagr Tyagi Jul 12 '18 at 05:22
  • You please check this answer. If you are following a wrong answer that is not appreciable. – Khemraj Sharma Jul 12 '18 at 05:25
  • https://stackoverflow.com/a/12512783/6891563 This answer is by CommonsWare sir, you please follow right methods. – Khemraj Sharma Jul 12 '18 at 05:28
  • I am sorry, but I really don't see any difference between what I have done and CommonsWare's solutions you have posted. And my NotificationBootReceiver class does not run automatically on reboot. Please help. – Kushagr Tyagi Jul 12 '18 at 05:41
  • 1
    Strong solution is using Work Manger which was recently launched, You can also use Evernote Jobs which will internally use Work Manager from now. Because Alarm Mangers are not reliable. – Khemraj Sharma Jul 12 '18 at 05:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174858/discussion-between-kushagr-tyagi-and-khemraj). – Kushagr Tyagi Jul 12 '18 at 05:45