Workaround for local notifications on Android when app is closed and power saver mode is activated

Setup:

  • Unity 2021.3.2f1 Basic empty new 2D
  • URP project Mobile Notifications package
  • Sample scene from the Mobile Notification package
  • Built and ran on
    on a Samsung Galaxy note 20 ultra
    (but seems to apply to all the phones
    I have tested)

Code:
`

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Unity.Notifications.Android;
    
    public class NotificationControllerAndroid : MonoBehaviour
    {
    	public static bool initialized = false;
    	
    	public static bool Initialize() {
    		if(initialized == true) return true;
    		initialized = true;
    		var channel = new AndroidNotificationChannel() {
    			Id = "example_channel_id",
    			Name = "Example Notifications",
    			Importance = Importance.Default,
    			Description = "Generic notifications",
    		};
    		AndroidNotificationCenter.RegisterNotificationChannel(channel);
    
    		return true;
    	}
    
    
    	public static void SendNotification(int id, int fireTime, string title, string message, Color color) {
    		Initialize();
    		var notification = new AndroidNotification();
    		notification.FireTime = System.DateTime.Now.AddMilliseconds(fireTime);
    		Debug.LogWarning(System.DateTime.Now);
    		Debug.LogError(notification.FireTime);
    		notification.Title = title;
    		notification.Text = message;
    		notification.Color = color;
    
    		AndroidNotificationCenter.SendNotificationWithExplicitID(notification, "example_channel_id", id);
    	}
    
    	public static bool CancelNotification(int id) {
    		Initialize();
    
    		if (AndroidNotificationCenter.CheckScheduledNotificationStatus(id) != NotificationStatus.Unknown) {
    			AndroidNotificationCenter.CancelNotification(id);
    			return true;
    		} else return false;
    	}
    
    	public static void CancelAllNotifications() {
    		Initialize();
    		AndroidNotificationCenter.CancelAllNotifications();
    	}
    }

`

And then elsewhere called simple like so:
`

NotificationControllerAndroid.SendNotification(100, 10000, "Example", "Example text", new Color32(0, 0, 0, 255));

`

Alright, so all of this normally works fine. The rub comes in when power saver mode is enabled and the application is closed. My understanding is this is a multifaceted problem. If an app is unfocused, such as changing apps, then it is still running in the background and can still show notifications. Works as intended. If it is closed from the recent app menu it would still schedule and display the notification. Works as intended. If force stopped then no notification is shown, but again, seems like intended behavior. However, when power saver mode is activated, and an app is closed from the recent app menu it will not display the notification until either power saver mode is turned off or the application is reopened. Now it seems like this is a measure to help save battery, I am just wondering, are there any good workarounds? Preferably something I can set per-case message and fire times for, from inside the application? Like say “notify the user their building is done after XXX amount of time”? Or perhaps a way to bypass the block from power saver mode?

Hi @Codster I was wondering did you ever find a solution to this problem? Having what sounds like the same issue on Samsung specifically.