Error: Namespace "iOS" does not exist in Unity.Notifications

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.)
9089095--1258714--Screen Shot 2023-06-18 at 6.57.55 PM.png
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);
        }
       
    }
}

The #if statements do work but they’re only changing the using directives. There’s no magic that makes all the code using types coming from these using directives disappear. Just like if you’d remove using UnityEngine;, you’ll get lots of error because all the Unity types can no longer be found.

What you need to do is to not only put the using statements in an #if block but also all the code that is using types from those using directives. e.g. all Android-specific code needs to be in an #if UNITY_ANDROID block.

This gets messy and there’s no simple way to avoid that. You can try to design a system where the platform-dependent code is separated and there’s something coordinating the different platform implementations – but that’s also not straightforward and usually not worth it for simple cases.

DID YOU RESOLVE? I have the same issue…

https://stackoverflow.com/questions/56349026/error-namespace-ios-does-not-exist-in-unity-notifications
Check this, you should also use conditions for your method as notifications.android doesn’t have such classes