Search code examples
androidservicenotificationsalarmmanagersystem

Notification not showing because service or alarm manager is killed off


I have a problem I can't wrap my head around, I spent 7 hours today trying to find the solution. I am trying display notification for my app. It works great on older devices with older OS versions, however on Note 8 Running Oreo API 27, my notifications do not show. I spent hours and hours of testing and I found the issue: While application is running and app is open, notification will show, however when I exit the application, notifications don't show. So I figured it has something to do with the way system is handling services.

Is this normal behavior? Is there a way around this?

This is my code for notification:

onCreate...
 if (settings.getBoolean("enabled", true)) {
            if (settings.getLong("lastRun", Long.MAX_VALUE) < System.currentTimeMillis() - mTimeDelay) {
                sendNotification();
            }
        }

        setAlarm();
        stopSelf();
}
  public void setAlarm() {

        Intent serviceIntent = new Intent(this, CheckRecentRun.class);
        PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + mTimeDelay, pi);
        //Alarm is set for 3 days
    }

    public void sendNotification() {
        Intent intent = new Intent(this, WelcomeActivity.class);
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);



        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = notificationManager.getNotificationChannel(channelId);
            if (mChannel == null) {
                mChannel = new NotificationChannel(channelId, channelName, importance);
                mChannel.setDescription(mNotificationText);
                mChannel.enableLights(false);
                mChannel.setLightColor(Color.GREEN);
                mChannel.setShowBadge(false);
                notificationManager.createNotificationChannel(mChannel);
            }
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId).setStyle(new NotificationCompat.BigTextStyle().bigText(mNotificationText)).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(mAppName).setContentText(mNotificationText).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(mNotificationCode, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify(notificationId, mBuilder.build());
    }

Solution

  • It may be Samsung's aggressive battery management behaviour (among many other brands like Huawei) on their newer phones killing your app.

    Check out this website: https://dontkillmyapp.com/

    And look at the notes under Samsung:

    Galaxy S8 and later With the introduction of their flagship Galaxy S8 (and with some earlier experiments), Samsung has introduced a flawed attempt at prolonging battery life called App power monitor.

    For your apps to work correctly, please whitelist them in App power monitor.

    How to do it:

    Open the Settings > Device maintenance > Battery and at the bottom you’ll see a list of your most frequently used apps. You can manage apps individually or in a group by selecting them then tapping the big Save power button. Apps that are sleeping will appear in the Sleeping apps list at the bottom (tap it to expand the list). Scrolling further — all the way to the very bottom — and you’ll find Unmonitored apps. These are apps that you specifically want to exclude (white list) from App power monitor evil reach.

    When inside the Unmonitored apps menu, you can tap the 3-dot menu to add or delete apps from the list. Rather than bothering with any of that, you can just turn off the App power monitor feature completely as it has little-to-no impact on battery life and only serves to handicap the normal functioning of your Galaxy phone.

    It’s excessive and in some cases downright misleading, using scare tactics to keep you reliant on Samsung’s software when other Android devices get by just fine without it.

    Try this first.. Let me know if there is still any issue after this.

    If your code works on other devices or on your emulator, this should be the issue.