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.