0

I have a basic unity scene that involves a button which is linked to an OnClick method. I know this part works because I have the debug.log that tells me.

I tried looking all over the internet and stack overflow and couldn't find an answer to my problem.

The part that I suspect is the problem is the authorization request. I'm using Unity simulator for testing as well as the remote 5 app. When I play my project there is no request on either testing form for notifications. So my main question is why does the authorization request fail. I am using the mobile notifications package offered by Unity and have tried messing around with the settings there such as request authorization on app launch.

Part of what confuses me is that I got most of the code straight from Unity documentation.

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Notifications.iOS;
using UnityEngine;

public class NotificationManager : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(RequestAuthorization());
    }
    IEnumerator RequestAuthorization()
    {
        using (var req = new AuthorizationRequest(AuthorizationOption.Alert | AuthorizationOption.Badge, true))
        {
            Debug.Log(req.IsFinished);
            while (!req.IsFinished)
            {
                yield return null;
            };

            string res = "\n RequestAuthorization:";
            res += "\n finished: " + req.IsFinished;
            res += "\n granted :  " + req.Granted;
            res += "\n error:  " + req.Error;
            res += "\n deviceToken:  " + req.DeviceToken;
            Debug.Log(res);
        }
    }
    
    public void OnClick()
    {
        Debug.Log("Sending Notif");
        SendNotification();
    }
    
    private void SendNotification()
    {
        var timeTrigger = new iOSNotificationTimeIntervalTrigger()
        {
            TimeInterval = new TimeSpan(0, 0, 1),
            Repeats = false
        };

        var notification = new iOSNotification()
        {
            // You can specify a custom identifier which can be used to manage the notification later.
            // If you don't provide one, a unique string will be generated automatically.
            Identifier = "testNotification",
            Title = "Test Notification",
            Body = "Testing the notification capability",
            Subtitle = "What does the subtitle look like",
            ShowInForeground = true,
            ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
            CategoryIdentifier = "category_a",
            ThreadIdentifier = "thread1",
            Trigger = timeTrigger,
        };

        iOSNotificationCenter.ScheduleNotification(notification);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Sammytron
  • 1
  • 1
  • 1
  • 1
    Have you tried to build and run it directly on the phone? Not sure but I guess with the Unity remote is that the code is still basically executed on your PC where the Authorization doesn't exist – derHugo May 23 '21 at 07:09
  • @derHugo That's what I was suspecting and afraid of. That makes it hard to test notifications. The reason I'm annoyed is because I've never built an iOS app before and it looks way more complicated than windows. – Sammytron May 23 '21 at 14:18
  • @derHugo This is even more frustrating because the Unity Cloud Build site is down currently. And when I try to use a mac from 8 years ago which is really slow, it looks like I need to buy the developer kit. Just to test notifications. Why isn't it easier to test notifications. – Sammytron May 23 '21 at 15:21
  • Never have build for iOS .. but well seems like it is how it is ^^ most of the times you need to test these platform specific things directly on the target device since it is pretty hard to emulate them without emulating the entire OS ;) – derHugo May 23 '21 at 16:03
  • Your code was running for me...on physical phone (my strategy is to request later the permission). If you want to run the request at startup, you can check "Request Authorization on App Launch" under Mobile Notification settings. – Stefan Ciobotaru Jul 02 '22 at 22:01

0 Answers0