Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Duplicate Notifications

Discussion in 'Android' started by denissuu, Oct 26, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    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 :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Notifications.Android;
    5. using System;
    6.  
    7. public class NotificationManager : MonoBehaviour
    8. {
    9.     #region Notification Channels
    10.  
    11.     private AndroidNotificationChannel default_channel;
    12.     private AndroidNotificationChannel reminder_channel;
    13.  
    14.     #endregion
    15.  
    16.  
    17.     #region Daily Rewards Variables
    18.  
    19.     private NotificationStatus DailyReward_notificationStatus;
    20.     private int DailyReward_ID;
    21.     private AndroidNotification DailyReward_Reminder;
    22.  
    23.     #endregion
    24.  
    25.     #region Reminder to Play
    26.  
    27.     private NotificationStatus PlayReminder_notificationStatus;
    28.     private int PlayReminder_ID;
    29.     private AndroidNotification PlayReminder;
    30.  
    31.     #endregion
    32.  
    33.     // Start is called before the first frame update
    34.  
    35.     void Start()
    36.     {
    37.         #region Channels
    38.  
    39.         default_channel = new AndroidNotificationChannel() // Used for push notifications, events, discounts etc
    40.         {
    41.             Id = "default_channel_id",
    42.             Name = "General Channel",
    43.             Importance = Importance.Default,
    44.             EnableVibration = true,
    45.             EnableLights = true,
    46.             Description = "Used for normal notifications",
    47.         };
    48.         default_channel.LockScreenVisibility = LockScreenVisibility.Public;
    49.         AndroidNotificationCenter.RegisterNotificationChannel(default_channel);
    50.  
    51.         reminder_channel = new AndroidNotificationChannel() // Used for reminders like daily reward or checking back to play
    52.         {
    53.             Id = "reminder_channel_id",
    54.             Name = "Reminder Channel",
    55.             Importance = Importance.High,
    56.             EnableLights = true,
    57.             EnableVibration = true,
    58.             Description = "Notifications to remind the player",
    59.         };
    60.         reminder_channel.LockScreenVisibility = LockScreenVisibility.Public;
    61.         AndroidNotificationCenter.RegisterNotificationChannel(reminder_channel);
    62.  
    63.  
    64.         AndroidNotificationCenter.CancelScheduledNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
    65.         AndroidNotificationCenter.CancelNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
    66.         AndroidNotificationCenter.CancelDisplayedNotification(PlayReminder_ID); // Cancel it before rescheduling/sending again so we don't get duplicate notifications
    67.  
    68.         #endregion
    69.  
    70.         #region Daily Reward
    71.         DailyReward_Reminder = new AndroidNotification();
    72.         DailyReward_Reminder.Title = "Daily Reward";
    73.         DailyReward_Reminder.Text = "Your Daily Reward is ready to be claimed!";
    74.         DailyReward_Reminder.FireTime = System.DateTime.Now.AddSeconds(25);
    75.         DailyReward_Reminder.LargeIcon = "dailyreward_large";
    76.  
    77.  
    78.         CheckDailyReward_NotificationStatus(); // Check notification status for daily rewards
    79.         #endregion
    80.  
    81.  
    82.         #region Play Reminder
    83.         PlayReminder = new AndroidNotification();
    84.         PlayReminder.Title = "Pssst, over here!";
    85.         PlayReminder.Text = "You're missing out on a bunch of things, come check them out!";
    86.         PlayReminder.FireTime = System.DateTime.Now.AddDays(3);
    87.         PlayReminder.RepeatInterval = TimeSpan.FromDays(3);
    88.         PlayReminder.Style = NotificationStyle.BigTextStyle;
    89.  
    90.         PlayReminder_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(PlayReminder_ID);
    91.  
    92.         if (PlayReminder_notificationStatus != NotificationStatus.Scheduled)
    93.         {
    94.             PlayReminder_ID = AndroidNotificationCenter.SendNotification(PlayReminder, "reminder_channel_id");
    95.             Debug.Log("Notification existing, deleting sending another Play Reminder Notification..");
    96.         }
    97.  
    98.  
    99.         CheckPlayReminder_NotificationStatus();
    100.  
    101.         #endregion
    102.     }
    103.  
    104.     #region Play Reminder
    105.  
    106.     void CheckPlayReminder_NotificationStatus()
    107.     {
    108.         PlayReminder_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(PlayReminder_ID);
    109.         Debug.Log("Starting Play Reminder Notification Checks..");
    110.         if (PlayReminder_notificationStatus == NotificationStatus.Delivered)
    111.         {
    112.             // Remove the previously shown notification from the status bar.
    113.             Debug.Log("Play Reminder Notification already delivered, removing the notification if activated..");
    114.             AndroidNotificationCenter.CancelNotification(PlayReminder_ID);
    115.         }
    116.     }
    117.  
    118.  
    119.     #endregion
    120.  
    121.  
    122.  
    123.     #region Daily Rewards
    124.     public void DailyRewardNotificationTrigger() // For manual triggers
    125.     {
    126.  
    127.         DailyReward_ID = AndroidNotificationCenter.SendNotification(DailyReward_Reminder, "reminder_channel_id");
    128.  
    129.         Debug.Log("Notification Sent");
    130.     }
    131.  
    132.     private void CheckDailyReward_NotificationStatus()
    133.     {
    134.         DailyReward_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(DailyReward_ID);
    135.  
    136.         Debug.Log("Starting Daily Reward Reminder Notification Checks..");
    137.         if (DailyReward_notificationStatus == NotificationStatus.Delivered)
    138.         {
    139.             // Remove the previously shown notification from the status bar.
    140.             Debug.Log("Daily Reward Notification already delivered, removing the notification..");
    141.             AndroidNotificationCenter.CancelNotification(DailyReward_ID);
    142.         }
    143.     }
    144.     #endregion
    145. }
    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! :)
     
    egeatici likes this.
  2. Trungdv

    Trungdv

    Joined:
    Dec 1, 2012
    Posts:
    22
    I have the same problem. Does anyone know the solution?
     
    egeatici likes this.
  3. mech_alina

    mech_alina

    Joined:
    Nov 3, 2020
    Posts:
    26
    I think because your firetime is a add delay already, you don't need the repeat interval at all.
     
    awsapps likes this.
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,624
    This is your problem. If you remove your application from recent apps, you are killing it and relaunching, so any values in your variables are lost.
    For such operation it's best to define your own ID as a constant and assign it to notification manually, then your cancelling will work.
     
  5. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    Can I get a further explanation on this? I keep getting different IDs per notification and I cannot seem to assign them properly
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,624
    How are you assigning them?