Hello! I am running into a very frustrating problem in a Unity C# Project of mine.
I am trying to make a script that will send a notification to android and iOS devices. However, I have noticed that putting both systems into one script may not work properly…
Here’s the problem:
using Unity.Notifications.iOS;
using Unity.Notifications.Android;
This piece of code ends up clashing with itself, because I would assume iOS devices can’t use “Unity.Notifications.Android”. So, I looked up a solution, and I found this:
#if UNITY_IOS
using Unity.Notifications.iOS;
#elif UNITY_ANDROID
using Unity.Notifications.Android;
#endif
This seemed to work for other people, so I tried it out. But, for some reason, it is not functioning like it would in the example. (See screenshot.)

I noticed that the iOS section is not grayed-out. I just brushed it off and continued though. But, errors started showing up all over my code!
It was acting as if the Unity.Notifications.iOS; didn’t even exist! I thought it may be a problem with the build settings, so I switched over to iOS building, and the errors went away. But then, errors showed up on the other half of my code!
The #if statements at the top were essentially doing nothing, and I have no idea how to fix this issue. Does anybody know the solution, or may have some ideas for why it could be doing this? Thanks!
Full code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#if UNITY_IOS
using Unity.Notifications.iOS;
#elif UNITY_ANDROID
using Unity.Notifications.Android;
#endif
using UnityEngine;
using UnityEngine.UI;
public class Notification : MonoBehaviour
{
public string notificationText;
public string notificationTextOld;
// Start is called before the first frame update
private void Update()
{
if (gameObject.GetComponent<Text>().text != notificationText)
{
notificationText = gameObject.GetComponent<Text>().text;
Debug.Log(notificationText);
SendNotification();
}
}
public void SendNotification()
{
if (SystemInfo.operatingSystem == "Android")
{
var c = new AndroidNotificationChannel()
{
Id = "channel_id",
Name = "Default Channel",
Importance = Importance.High,
Description = "Generic notifications",
};
AndroidNotificationCenter.RegisterNotificationChannel(c);
var notificationandroid = new AndroidNotification();
notificationandroid.Title = "JESHCOM";
notificationandroid.Text = notificationText;
notificationandroid.LargeIcon = "jesh_large";
notificationandroid.FireTime = System.DateTime.Now;
AndroidNotificationCenter.SendNotification(notificationandroid, "channel_id");
}
if (SystemInfo.operatingSystem == "iOS")
{
IEnumerator RequestAuthorization()
{
using (var req = new AuthorizationRequest(AuthorizationOption.Alert | AuthorizationOption.Badge, true))
{
while (!req.IsFinished)
{
yield return null;
};
string res = "\n RequestAuthorization: \n";
res += "\n finished: " + req.IsFinished;
res += "\n granted : " + req.Granted;
res += "\n error: " + req.Error;
res += "\n deviceToken: " + req.DeviceToken;
Debug.Log(res);
}
}
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = new TimeSpan(0, 10, 0),
Repeats = false
};
var notificationios = new iOSNotification()
{
// You can optionally specify a custom identifier which can later be
// used to cancel the notification, if you don't set one, a unique
// string will be generated automatically.
Identifier = "_notification_01",
Title = "JESHCOM",
Body = "Scheduled at: " + DateTime.Now.ToShortDateString() + " triggered in 5 seconds",
Subtitle = notificationText,
ShowInForeground = true,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = "category_a",
ThreadIdentifier = "thread1",
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notificationios);
}
}
}
