LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)

Solution 1:

Android O are pretty new to-date. Hence, I try to digest and provide as accurate possible information.

From https://developer.android.com/about/versions/oreo/background.html#broadcasts

  • Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
    • Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.
  • Apps can continue to register explicit broadcasts in their manifest.

Also, in https://developer.android.com/training/scheduling/alarms.html , the examples are using explicit broadcast, and doesn't mention anything special regarding Android O.


May I suggest you try out explicit broadcast as follow?

public static void startAlarmBroadcastReceiver(Context context, long delay) {
    Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    // Remove any previous pending intent.
    alarmManager.cancel(pendingIntent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);        
}

AlarmBroadcastReceiver

public class AlarmBroadcastReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
    }

}

In AndroidManifest, just define the class as

<receiver android:name="org.yccheok.AlarmBroadcastReceiver" >
</receiver>

Solution 2:

Today i had the same problem and my notification was not working. I thought Alarm manager is not working in Oreo but the issue was with Notification. In Oreo we need to add Channel id. Please have a look into my new code:

int notifyID = 1; 
String CHANNEL_ID = "your_name";// The id of the channel. 
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(HomeActivity.this)
            .setContentTitle("Your title")
            .setContentText("Your message")
            .setSmallIcon(R.drawable.notification)
            .setChannelId(CHANNEL_ID)
            .build();

Check this solution. It worked like charm.

https://stackoverflow.com/a/43093261/4698320

I am showing my method:

public static void pendingListNotification(Context context, String totalCount) {
        String CHANNEL_ID = "your_name";// The id of the channel.
        CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationCompat.Builder mBuilder;

        Intent notificationIntent = new Intent(context, HomeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.PENDING_NOTIFICATION, AppConstant.TRUE);//PENDING_NOTIFICATION TRUE
        notificationIntent.putExtras(bundle);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= 26) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mNotificationManager.createNotificationChannel(mChannel);
            mBuilder = new NotificationCompat.Builder(context)
//                .setContentText("4")
                    .setSmallIcon(R.mipmap.logo)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setLights(Color.RED, 300, 300)
                    .setChannelId(CHANNEL_ID)
                    .setContentTitle(context.getResources().getString(R.string.yankee));
        } else {
            mBuilder = new NotificationCompat.Builder(context)
//                .setContentText("4")
                    .setSmallIcon(R.mipmap.logo)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setLights(Color.RED, 300, 300)
                    .setContentTitle(context.getResources().getString(R.string.yankee));
        }

        mBuilder.setContentIntent(contentIntent);

        int defaults = 0;
        defaults = defaults | Notification.DEFAULT_LIGHTS;
        defaults = defaults | Notification.DEFAULT_VIBRATE;
        defaults = defaults | Notification.DEFAULT_SOUND;

        mBuilder.setDefaults(defaults);
        mBuilder.setContentText(context.getResources().getString(R.string.you_have) + " " + totalCount + " " + context.getResources().getString(R.string.new_pending_delivery));//You have new pending delivery.
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

Solution 3:

Create the AlarmManager by defining an explicit intent (explicitly define the class name of the broadcast receiver):

private static PendingIntent getReminderReceiverIntent(Context context) {
    Intent intent = new Intent("your_package_name.ReminderReceiver");
    // create an explicit intent by defining a class
    intent.setClass(context, ReminderReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingIntent;
}

Also do not forget to create a notification channel for Android Oreo (API 26) when creating the actual notification:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (VERSION.SDK_INT >= VERSION_CODES.O) {
    notificationManager.createNotificationChannel(NotificationFactory.createNotificationChannel(context));
} else {
    notificationManager.notify(NotificationsHelper.NOTIFICATION_ID_REMINDER, notificationBuilder.build());
}

Solution 4:

try this code for android O 8.1

Intent nIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        nIntent.addCategory("android.intent.category.DEFAULT");
        nIntent.putExtra("message", "test");
        nIntent.setClass(this, AlarmReceiver.class);

PendingIntent broadcast = PendingIntent.getBroadcast(getAppContext(), 100, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);