Android Notifications

Hi, I’m following the Mobile Game Development course, lesson 30, Android notifications.

Once I install the app and use the 5 energies, there is no notification coming up, if I close and reopen the app after 1 minute the energies are replenished and the score is still saved. Also if on the phone(Redimi 8) I go to Manage apps , click on the app and go in the notification I can see there is a notification channel. I tried to remove the FTAs, since I saw a few had a spelling issue that was causing the notification system to not work.

Main Menu Code:

public class MainMenu : MonoBehaviour
{

    [SerializeField] TMP_Text highScoreText;
    [SerializeField] Text playText;
    [SerializeField] int maxEnergy;
    [SerializeField] int energyRechargeDuration;
    [SerializeField] AndroidNotificationHandler androidNotificationHandler;

    int energy;

    private const string EnergyKey = "Energy";
    private const string EnergyReadyKey = "EnergyReady";
    


   public void Play()
   {
        if (energy < 1) return;
    energy --;
    PlayerPrefs.SetInt(EnergyKey, energy);

    if(energy == 0)
    {
            DateTime energyReady = DateTime.Now.AddMinutes(energyRechargeDuration);
            PlayerPrefs.SetString(EnergyReadyKey, energyReady.ToString());
           #if UNITY_ANDROID
            androidNotificationHandler.ScheduleNotification(energyReady);
            #endif

    }
            SceneManager.LoadScene(1);
   }

   private void Awake() 
   {

    int highscore= PlayerPrefs.GetInt(Score.highScoreKey, 0);
    highScoreText.text = $"High Score: {highscore}";

    energy = PlayerPrefs.GetInt(EnergyKey, maxEnergy);

    if(energy == 0)
    {
        string energyReadyString = PlayerPrefs.GetString(EnergyReadyKey, string.Empty);

        if(energyReadyString == string.Empty) return;

        DateTime energyReady = DateTime.Parse(energyReadyString);

        if(DateTime.Now > energyReady)
        {
            energy = maxEnergy;
            PlayerPrefs.SetInt(EnergyKey, energy);
        }

        playText.text = PlayerPrefs.GetString(EnergyReadyKey);
    }
    

    playText.text = $"Play ({energy})";

   }
}

NotificationHandler Code:

public class AndroidNotificationHandler : MonoBehaviour
{
#if UNITY_ANDROID
    private const string ChannelID ="notification_channel";
  public void ScheduleNotification(DateTime detaTime)
  {
    AndroidNotificationChannel notificationChannel = new AndroidNotificationChannel
    {
        Id = ChannelID,
        Name = "Notification Channel",
        Description = "Come Back",
        Importance = Importance.Default
    };

    AndroidNotificationCenter.RegisterNotificationChannel(notificationChannel);

    AndroidNotification notification = new AndroidNotification
    {
        Title = "Energy Recharged",
        Text = " You can play again!",
        SmallIcon = "icon_0",
        LargeIcon = "icon_1",
        FireTime = detaTime

    };

    AndroidNotificationCenter.SendNotification(notification, ChannelID);
  }
#endif
}

Thanks in advance.

Also, I just notice that when building, I get this error :

NotSupportedException: Minimum Android API level supported by notifications package is 21, your Player Settings have it set to AndroidApiLevel19
Unity.Notifications.AndroidNotificationPostProcessor.MinSdkCheck () (at Library/PackageCache/com.unity.mobile.notifications@2.1.1/Editor/AndroidNotificationPostProcessor.cs:36)
Unity.Notifications.AndroidNotificationPostProcessor.OnPostGenerateGradleAndroidProject (System.String projectPath) (at Library/PackageCache/com.unity.mobile.notifications@2.1.1/Editor/AndroidNotificationPostProcessor.cs:19)
UnityEditor.Android.AndroidBuildPipelineInterfaces.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <25578071f6e44201aac745680e5c8dfc>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

So I went to File-Build Settings- Player Settings - Minimum API Level and set it to Level 21 and this fixed the problem. Now notifications are showed correctly.

This information may actually help with other students running into the same problem!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms