Failing to send notifications in Oreo on boot?


#1

I already figured out the NotificationChannel detail thanks to this post, so notifications are working every minute after successfully launching the app. But after getting through the “Using Receivers” section, the notifications don’t appear on boot.

My Android.manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bignerdranch.android.photogallery">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".PhotoGalleryActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <service android:name=".PollService"/>

    <receiver android:name=".StartupReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>

</manifest>

The Startup Receiver:

public class StartupReceiver extends BroadcastReceiver {
    private static final String TAG = "StartupReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Received broadcast intent: " + intent.getAction());

        boolean isOn = QueryPreferences.isAlarmOn(context);
        Log.i(TAG, "Turning the service alarm " + (isOn ? "on" : "off"));

        PollService.setServiceAlarm(context, isOn);
    }
}

If I filter the logs in the Android Device Monitor by this app I can see that my app is definitely getting the BOOT_COMPLETED intent and telling the PollService to turn the alerts back on

12-08 10:47:51.069: I/StartupReceiver(4421): Received broadcast intent: android.intent.action.BOOT_COMPLETED
12-08 10:47:51.088: I/StartupReceiver(4421): Turning the service alarm on
12-08 10:47:51.092: I/PollService(4421): Setting service alarm to true

Here’s what that method loos like inside PollService:

public static void setServiceAlarm(Context context, boolean isOn) {
    Intent i = PollService.newIntent(context);
    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (isOn) {
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), POLL_INTERVAL_MS, pi);
    } else {
        alarmManager.cancel(pi);
        pi.cancel();
    }

    Log.i(TAG, "Setting service alarm to " + isOn);
    QueryPreferences.setIsAlarmOn(context, isOn);
}

So… what? I know Oreo made some changes to these broadcast intents, but it looks like the BOOT_COMPLETED intent is unaffected and I’m receiving it successfully. Did they change AlarmManager somehow?

Again, that same function call from the running app successfully schedules the repeating notification, so I know that code is good. I just can’t figure out why calling it in this way doesn’t have the same effect.


#2

@downie I am facing the same issue. Did you find the solution? Is this problem related to setting the alarm?