I think typed everything correctly why aren’t the Android notifications working? I am using Android 12 on my Samsung Galaxy Z Flip 3 5G and the latest Unity LTS version which is 2021.3.11f1 currently. Here is my Android Notification Handler script:
using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_ANDROID
using Unity.Notifications.Android;
#endif
using UnityEngine;
public class AndroidNotificationHandler : MonoBehaviour
{
#if UNITY_ANDROID
private const string ChannelId = "notification_channel";
public void ScheduleNotificaton(DateTime dateTime)
{
AndroidNotificationChannel notificationChannel = new AndroidNotificationChannel
{
Id = ChannelId,
Name = "Notification Channel",
Description = "the default Notification Channel",
Importance =Importance.Default
};
AndroidNotificationCenter.RegisterNotificationChannel(notificationChannel);
AndroidNotification notification = new AndroidNotification
{
Title = "Energy Recharged",
Text = "Your energy has recharged, come back to play again!",
SmallIcon = "default",
LargeIcon = "default",
FireTime = dateTime
};
AndroidNotificationCenter.SendNotification(notification, ChannelId);
}
#endif
}
And here is the Main Menu Script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
[SerializeField] private TMP_Text highScoreText;
[SerializeField] private TMP_Text energyText;
[SerializeField] private Button playButton;
[SerializeField] private AndroidNotificationHandler androidNotificationHandler;
[SerializeField] private int maxEnergy;
[SerializeField] private int energyRechargeDuration;
private int energy;
private const string EnergyKey = "Energy";
private const string EnergyReadyKey = "EnergyReady";
private void Start()
{
OnApplicationFocus(true);
}
private void OnApplicationFocus(bool hasFocus)
{
if(!hasFocus)
{
return;
}
CancelInvoke();
float currenthighcore = PlayerPrefs.GetInt(ScoreSystem.HighScoreKey, 0);
highScoreText.text = $"High Score: {currenthighcore}";
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)
{
playButton.interactable = true;
energy = maxEnergy;
PlayerPrefs.SetInt(EnergyKey, energy);
}
else
{
playButton.interactable = false;
Invoke(nameof(EnergyRecharged), (energyReady - DateTime.Now).Seconds);
}
}
energyText.text = $"Play (Energy {energy})";
}
private void EnergyRecharged()
{
playButton.interactable = true;
energy = maxEnergy;
PlayerPrefs.SetInt(EnergyKey, energy);
energyText.text = $"Play (Energy {energy})";
}
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.ScheduleNotificaton(energyReady);
#endif
}
SceneManager.LoadScene(1);
}
}
Did I type something wrong? Am I missing a request for permission or do I need a Google Developer account make the notifications work? Please help.