Hi there! I’ve been trying to solve this issue for about 6 hours and I can’t seem to fix it.I wanna have a notification that repeats every 3 days, but I am facing a weird issue.I open my application, the notification displays properly, but if I close the application and remove it from recent apps and then open it again, now it sends 2 of the same notification at a time.
I am using repeatInterval to repeat this notification every 3 days, but I don’t know how I can prevent it from not starting again if the repeated notification was triggered already in a previous session, or detect that it has been triggered and not try sending another repeated notification again.
The notification that I’m having problems with is the PlayReminder.
Here’s the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Notifications.Android;
using System;
public class NotificationManager : MonoBehaviour
{
#region Notification Channels
private AndroidNotificationChannel default_channel;
private AndroidNotificationChannel reminder_channel;
#endregion
#region Daily Rewards Variables
private NotificationStatus DailyReward_notificationStatus;
private int DailyReward_ID;
private AndroidNotification DailyReward_Reminder;
#endregion
#region Reminder to Play
private NotificationStatus PlayReminder_notificationStatus;
private int PlayReminder_ID;
private AndroidNotification PlayReminder;
#endregion
// Start is called before the first frame update
void Start()
{
#region Channels
default_channel = new AndroidNotificationChannel() // Used for push notifications, events, discounts etc
{
Id = "default_channel_id",
Name = "General Channel",
Importance = Importance.Default,
EnableVibration = true,
EnableLights = true,
Description = "Used for normal notifications",
};
default_channel.LockScreenVisibility = LockScreenVisibility.Public;
AndroidNotificationCenter.RegisterNotificationChannel(default_channel);
reminder_channel = new AndroidNotificationChannel() // Used for reminders like daily reward or checking back to play
{
Id = "reminder_channel_id",
Name = "Reminder Channel",
Importance = Importance.High,
EnableLights = true,
EnableVibration = true,
Description = "Notifications to remind the player",
};
reminder_channel.LockScreenVisibility = LockScreenVisibility.Public;
AndroidNotificationCenter.RegisterNotificationChannel(reminder_channel);
AndroidNotificationCenter.CancelScheduledNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
AndroidNotificationCenter.CancelNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
AndroidNotificationCenter.CancelDisplayedNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
#endregion
#region Daily Reward
DailyReward_Reminder = new AndroidNotification();
DailyReward_Reminder.Title = "Daily Reward";
DailyReward_Reminder.Text = "Your Daily Reward is ready to be claimed!";
DailyReward_Reminder.FireTime = System.DateTime.Now.AddSeconds(25);
DailyReward_Reminder.LargeIcon = "dailyreward_large";
CheckDailyReward_NotificationStatus(); // Check notification status for daily rewards
#endregion
#region Play Reminder
PlayReminder = new AndroidNotification();
PlayReminder.Title = "Pssst, over here!";
PlayReminder.Text = "You're missing out on a bunch of things, come check them out!";
PlayReminder.FireTime = System.DateTime.Now.AddDays(3);
PlayReminder.RepeatInterval = TimeSpan.FromDays(3);
PlayReminder.Style = NotificationStyle.BigTextStyle;
PlayReminder_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(PlayReminder_ID);
if (PlayReminder_notificationStatus != NotificationStatus.Scheduled)
{
PlayReminder_ID = AndroidNotificationCenter.SendNotification(PlayReminder, "reminder_channel_id");
Debug.Log("Notification existing, deleting sending another Play Reminder Notification..");
}
CheckPlayReminder_NotificationStatus();
#endregion
}
#region Play Reminder
void CheckPlayReminder_NotificationStatus()
{
PlayReminder_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(PlayReminder_ID);
Debug.Log("Starting Play Reminder Notification Checks..");
if (PlayReminder_notificationStatus == NotificationStatus.Delivered)
{
// Remove the previously shown notification from the status bar.
Debug.Log("Play Reminder Notification already delivered, removing the notification if activated..");
AndroidNotificationCenter.CancelNotification(PlayReminder_ID);
}
}
#endregion
#region Daily Rewards
public void DailyRewardNotificationTrigger() // For manual triggers
{
DailyReward_ID = AndroidNotificationCenter.SendNotification(DailyReward_Reminder, "reminder_channel_id");
Debug.Log("Notification Sent");
}
private void CheckDailyReward_NotificationStatus()
{
DailyReward_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(DailyReward_ID);
Debug.Log("Starting Daily Reward Reminder Notification Checks..");
if (DailyReward_notificationStatus == NotificationStatus.Delivered)
{
// Remove the previously shown notification from the status bar.
Debug.Log("Daily Reward Notification already delivered, removing the notification..");
AndroidNotificationCenter.CancelNotification(DailyReward_ID);
}
}
#endregion
}
I used a 20 seconds fire time and 1 minute repeat interval for testing purposes on the PlayReminder notification.
Tried canceling the previous notification before sending another one ( like above ), tried rescheduling, tried everything.
Can someone get me out of this issue with a fix?
I cannot get it to work.
Thanks for the help!