Matheus
Matheus

Reputation: 33

How to prevent Firebase Cloud Messaging ask for push notifications permission on iOS/Unity

I've been working on a project in Unity and I am having a problem with iOS.

I already have local push notifications implemented in my game and it asks for permission to deliver notifications later in the game. After I implemented Firebase Cloud Messaging, this order changed. Now, when the game starts the notifications permission prompt appears.

Is there a way to prevent Firebase requesting notifications permission when the game starts? I would like to ask for permission later in the game.

Thanks in advance.

Upvotes: 3

Views: 4974

Answers (3)

Krzysztof Turek
Krzysztof Turek

Reputation: 136

Each call with FirebaseMessaging reference, like FirebaseMessaging.TokenReceived or FirebaseMessaging.MessageReceived trigger the permission dialog. So you have to delay that call as long as possible. Settings FirebaseMessagingAutoInitEnabled to false doesn't help

Upvotes: 1

Tricko
Tricko

Reputation: 551

Make a PostProcessBuild where you can edit the info.plist generated XCode project and add an entry setting FirebaseMessagingAutoInitEnabled to false. You can check this out for more info on how to add/edit the plist.

Also, move any reference to FirebaseMessaging to the part of your game where you want to ask for permissions.

//If platform is not IOS, immediately set message listeners
#if !UNITY_IOS
SetPushNotificationListeners()
#endif

//Call this in the part of the game where you want the notifications to appear
public void SetPushNotificationListeners()
{
    //You might need FirebaseMessaging.RequestPermissionAsync() for IOS as well

    FirebaseMessaging.TokenReceived -= OnTokenReceived;
    FirebaseMessaging.TokenReceived += OnTokenReceived;

    FirebaseMessaging.MessageReceived -= OnMessageReceived;
    FirebaseMessaging.MessageReceived += OnMessageReceived;
}

I got the FirebaseMessagingAutoInitEnabled from the documentation but in my previous tests, it seems like having any reference to FirebaseMessaging will still ask for permissions. Also take note that this delays the initialisation of FirebaseMessaging and requesting for permissions so you might have to relaunch the game before actually receiving any push notifications (I haven't tested it this far so I am not sure).

Upvotes: 1

Mike Dattolo
Mike Dattolo

Reputation: 139

Initializing Firebase won't trigger the permission dialog. You will want to check where in your code you are requesting authorization to display notifications.

Assuming iOS 10 or later, look for the requestAuthorization call on UNUserNotificationCenter. That's what will trigger the ask for push notifications.

Upvotes: 1

Related Questions