Android Notification

how to solve this?

Paste in your AndroidNotificationHandler.cs script and we’ll take a look.
Be sure to paste the text of the script, rather than using a screenshot.

here it is,

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

#if UNITY_ANDROID

using Unity.Notifications.Android;

#endif

public class AndroidNotificationHandler : MonoBehaviour
{
#if UNITY_ANDRIOD

    private const string ChannelId = "notification_channel";

    public void ScheduleNotification(DateTime dateTime)
    {
        AndroidNotificationChannel notificationChannel = new AndroidNotificationChannel
        {
           Id = ChannelId,
           Name = "Notification Channel",
           Description = " Some Random Description",
           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

}

of mainmenu.cs

using System.Collections;
using System.Collections.Generic;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using Unity.Notifications.Android;

public class MainMenu : MonoBehaviour
{
    [SerializeField] private TMP_Text highScoreText;
    [SerializeField] private TMP_Text energyText;
    [SerializeField] private AndroidNotificationHandler androidNotificationHandler;
    [SerializeField] private int maxEnergy;
    [SerializeField] private int energyRechargeDuration;

     private int energy;
    private DateTime energyReady;
    private const string EnergyKey = "Energy";
    private const string EnergyReadyKey = "EnergyReady";

    private void Start()
    {
        int highScore = PlayerPrefs.GetInt(ScoreSystem.HighScoreKey, 0);
        highScoreText.text = $"HighScore: {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);
            }
        }
        energyText.text = $"Play ({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.ScheduleNotification(energyReady);
        #endif
        }
    SceneManager.LoadScene(1);
   }
}

i can’t get you, can you explain please

Sorry for the delay on this.
Everything looks right enough, and I spent some time researching if there was any other AndroidNotificationHandler method somewhere else… then on this last look through the script, I noticed a small typo under the class declaration for AndroidNotificationHandler

#if UNITY_ANDRIOD

It’s hard to spot in the mix, this is not the droid you’re looking for. (pardon the pop culture reference). It needs to be

#if UNITY_ANDROID

I would remove the using Unity.Notifications.Android from the Main Menu script. It’s not needed, and since it’s not surrounded by an #if clause, it will cause compiling to fail if you build for another platform other than android.

1 Like

I am so naive and dumb. :sweat_smile: :grin:

thank you so much!!! :pray: :v:

This sort of thing happens!
I went over that piece of code a dozen times trying to spot the error. I searched the Unity codebase for a hidden AndroidNotificationHandler somewhere… I was literally typing out a response apologizing for not being able to spot the error, typing that you must be using Android or the code wouldn’t even be throwing the error when I spotted it.

Hello everyone, first time here. First of all I’m loving this course but I do have a question. I’m trying to set both RepeatInterval and FireTime in the same AndroidNotification, is this possible? Because as soon as I try to implement the RepeatInterval my app stops sending notifications.

public void MyFirstNotification(DateTime dateTime, DateTime dateTime2, DateTime localTime)
   {
      AndroidNotification AndroidNotification_1 = new AndroidNotification
      {
         Title = "Title!",
         Text = "Random text",
         SmallIcon = "default",
         LargeIcon = "default",
         FireTime = dateTime,
         RepeatInterval = new TimeSpan(1, 0, 0, 0)
      };

      AndroidNotification AndroidNotification_2 = new AndroidNotification
      {
         Title = "Title!",
         Text = "Random text",
         SmallIcon = "default",
         LargeIcon = "default",
         FireTime = dateTime2,
         RepeatInterval = new TimeSpan(1, 0, 0, 0)
      };

      if(PlayerPrefs.GetInt(ChangeNotificationStage.NotificationKey) == 1 && localTime < dateTime)
      {
         AndroidNotificationCenter.SendNotification(AndroidNotification_1 , channelId);
      }
      else if(PlayerPrefs.GetInt(ChangeNotificationStage.NotificationKey) == 1 && localTime < dateTime2)
      {
         AndroidNotificationCenter.SendNotification(AndroidNotification_2, channelId);
      }
   }

From what I can see searching Unity’s forums and other sources, RepeatInterval does not seem to be working properly in Unity at this time.

I’m not sure what I got it wrong there but I think it’s working now.
I’ve manage to get multiple notification firing and all of them with an interval of 1 day.
I also had the problem of notifications stacking up if the user were to tap multiple times on the button. To avoid this I had to use the ShouldAutoCancel because I the CheckNotificationStatus does not work at all maybe this will be fixed on a future update but for now I’m happy with his workaround, the only problem is that the user will hear the notification sound multiple times if he had previously tap the button multiple times on that day but since the notifications won’t stack up on the mobile’s notifications I’ll just pretend it’s a feature xD

1 Like

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

Privacy & Terms