Максим
Максим

Reputation: 1

Unity Mobile Notifications not working on Android 13+

Notifications do not appear on Android 13 and above, but they work perfectly fine on Android 12.

The POST_NOTIFICATION permission is included in the custom manifest.

Before registering the notification channel and notifications themselves, I explicitly request the user's permission to send notifications.

According to Logcat, there are no errors, and the notifications are registered successfully.

I am using the latest version of UnityMobileNotification.

Interestingly, when I moved the script responsible for registering notifications to Unity 2022, adjusted the manifest, and tweaked the project settings, notifications worked correctly on all Android versions. The settings in the main project (Unity 6) and the test project (Unity 2022) appear to be identical.

Attaching the script:

    public async UniTask StartAsync(CancellationToken cancellation)
    {
        await WaitPermissionAsync(_tokenSource.Token);
        
        RegisterChannel();
        ClearNotifications();
    }

    private void RegisterChannel()
    {
        AndroidNotificationCenter.Initialize();
        var channel = new AndroidNotificationChannel()
        {
            Id = ChannelId,
            Name = "Notifications_Channel_Evolution",
            Description = "Default description",
            Importance = Importance.High,
        };

        AndroidNotificationCenter.RegisterNotificationChannel(channel);
        _notificationInitialized = true;

        Debug.Log("[Notification] Channel registered");
    }

    private async UniTask WaitPermissionAsync(CancellationToken token)
    {
        var request = new PermissionRequest();
        await UniTask.WaitUntil(() => request.Status != PermissionStatus.RequestPending, cancellationToken: token);

        Debug.Log("[Notification] Permission perform: " + request.Status);
    }

    private void RegisterNotifications()
    {
        if (_notificationInitialized == false)
        {
            return;
        }
        
        var calculatedTimes = CalculateNotificationTimes();
        int i = 0;
        foreach (var calculatedTime in calculatedTimes)
        {
            i++;
            
            if (i > 2)
            {
                return;
            }
            
            NotificationsType type = NotificationsType.Default;

            if (_dailyRewardClaimedObserver.GetDailyRewardStatus())
            {
                type = NotificationsType.Reward;
            }

            var notificationData = _localTextProvider.ChooseLocalKeysByRewardStatus(type)
                .GetRandomLocalText();

            var time = DateTime.Now.AddSeconds(20);

            var notification = new AndroidNotification
            {
                Title = notificationData.Item1,
                Text = notificationData.Item2,
                FireTime = time
            };

            var id = AndroidNotificationCenter.SendNotification(notification, ChannelId);
            var status = AndroidNotificationCenter.CheckScheduledNotificationStatus(id);

            Debug.Log($"[Notification] Notification {id} at {calculatedTime} is {status}");
        }

        Debug.Log("[Notification] Notifications registered");
    }

I tried transferring the project from Unity 6 (preview) to Unity 2022. As a result, notifications started working.

In the test project on Unity 2022, there was no Android Resolver or custom Gradle files.

Upvotes: 0

Views: 38

Answers (0)

Related Questions