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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple C# iOS Notification

Discussion in 'Scripting' started by ExbowFTW, Jun 30, 2017.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    My device asks me if I allow notifications, but even though I click "accept", the app still doesn't display notifications a minute after I startup the app.

    Is there a certain Xcode/Unity Player Preferences setting I have to do in order to make notifications work, or...?

    Edit: I've turned Notifications on in Xcode, btw.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using LocalNotification = UnityEngine.iOS.LocalNotification;
    5.  
    6. public class NotificationSystem : MonoBehaviour {
    7.  
    8.     UnityEngine.iOS.LocalNotification notif = new UnityEngine.iOS.LocalNotification();
    9.     LocalNotification newNotif;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (PlayerPrefs.GetInt ("New") == 0) //0 = New
    14.             RegisterForNotifications ();
    15.         else
    16.             PlayerPrefs.SetInt ("New", 1); //1 = Returning
    17.  
    18.         //Set new one 1 minute from now:
    19.         newNotif.fireDate = System.DateTime.Now.AddMinutes(1);
    20.         notif.alertBody = "Come back! Any time is the right time to practice your speaking skills!";
    21.         UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (newNotif);
    22.     }
    23.  
    24.     void RegisterForNotifications () {
    25.         UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert |
    26.         UnityEngine.iOS.NotificationType.Badge |
    27.         UnityEngine.iOS.NotificationType.Sound);
    28.     }
    29.  
    30.  
    31. }
     
    Last edited: Jun 30, 2017
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    You have 2 LocalNotification fields: notif and newNotif. newNotif is never instantiated so presumably you get a null reference exception at line 19?
     
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    newNotif actually is instantiated at Line 9. (so no error)

    Fyi: there are no errors except for 4 clear-able errors about InitWrapper and a NullRef at line 26 (fireDate)...
    Code (csharp):
    1.  
    2. newNotif.fireDate = System.DateTime.Now.AddMinutes(1);
    3.  
    What is wrong with this ^ ?
     
  4. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    This alone:

    Code (csharp):
    1.     LocalNotification newNotif;
    Does not instantiate newNotif. It's still null at this point.

    So if you were to try to access fireDate through newNotif (which is null), you get a NullRef error.

    You need to instantiate it first (just like you did with notif in the line above).
     
  5. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    So I instantiated it like you said above, but it still gives me NullRefExcep for Line 26 (27 in this case)
    Updated Code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using LocalNotification = UnityEngine.iOS.LocalNotification;
    6. //using UnityEngine.iOS;
    7.  
    8. public class NotificationSystem : MonoBehaviour {
    9.  
    10.     UnityEngine.iOS.LocalNotification notif = new UnityEngine.iOS.LocalNotification();
    11.     LocalNotification newNotif = new LocalNotification();
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         Screen.sleepTimeout = SleepTimeout.NeverSleep; //Stop Dimming
    16.  
    17.         if (PlayerPrefs.GetInt ("New") == 0) //0 = New
    18.             RegisterForNotifications ();
    19.         else
    20.             PlayerPrefs.SetInt ("New", 1); //1 = Returning
    21.  
    22.         if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0 || UnityEngine.iOS.NotificationServices.scheduledLocalNotifications.Length > 0) { //If open app & Notification Scheduled
    23.             UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications(); //Clear it
    24.         }
    25.  
    26.         //Set new one 1 minute from now:
    27.         newNotif.fireDate = System.DateTime.Now.AddMinutes(1);
    28.         notif.alertBody = "Come back! Any time is the right time to practice your speaking skills!";
    29.         UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (newNotif);
    30.     }
    31.  
    32.     void RegisterForNotifications () {
    33.         UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert |
    34.         UnityEngine.iOS.NotificationType.Badge |
    35.         UnityEngine.iOS.NotificationType.Sound);
    36.     }
    37.    
    38.  
    39. }
    40.  
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    For variables that are part of the class like that, you may need to reset it (or remove and re-add) the component to make it take effect. Or, you can just initialize it in Start() and be sure.
     
  7. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Aha okay! So I solved the error on Line 27 by doing Initialization at Start().

    However, I'm still getting this error: (Maybe it's because I'm on Mac , when Notif is for iOS?)
    Screen Shot 2017-06-30 at 11.52.25 AM.png
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Try moving the initialization for "notif" into Start, as well (and remove it from the class body).
     
  9. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    That fixed all the errors! Thanks!!!