firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
819 stars 424 forks source link

Android: Push notification messages are not received when the application is closed! #1334

Closed RaidelRoss closed 1 year ago

RaidelRoss commented 1 year ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

Notifications are not received when the application is closed. When the application is open, notifications are received, I get the notification id in OnMessageReceived When the application is minimized, notifications are received.

I've searched in forums and in issues but in none of them do I see that there is a solution. Reference is made to the documentation but I don't see that the people reporting the problem have resolved it.

Relevant Code:

This is how I register for events after Firebase is initialized.

public void InitializeFirebase()
    {
        Debug.Log("FBG:Start Register Event Messaging");
        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
        Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
        Firebase.Messaging.FirebaseMessaging.SubscribeAsync("TestTopic");
        Debug.Log("FBG:End Register Event Messaging");
    }

    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
        Debug.Log("FBG:Received Registration Token: " + token.Token);
    }

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
    {
        Debug.Log("FBG:Received a new message from: " + e.Message.From);
    }

Installing the package and subscribing to the events is not enough to receive messages when the application is closed, so my manifest ends up like this.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application>

        <service android:name="com.google.firebase.messaging.MessageForwardingService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name="com.google.firebase.MessagingUnityPlayerActivity"
            android:theme="@style/UnityThemeSelector">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>
    </application>
</manifest>
RaidelRoss commented 1 year ago

In this thread, I see replies stating that Firebase push notifications are not designed to work if the application is closed. Does this make sense? If so, what is the value of the com.google.firebase.messagingpackage? Is it intended for another function? issues:146

jonsimantov commented 1 year ago

Expected behavior in FCM is for the notification to appear in the system tray, but your Activity itself has to create the local notification to pop up a message when the remote notification comes in.

More info: https://firebase.google.com/docs/cloud-messaging/android/receive

RaidelRoss commented 1 year ago

I'm adding something important for users who are encountering the same problem. After trying many things, we simply created scheduled messages and suddenly, on some mobiles, notifications started to come through even if the app was closed.

In conclusion, there are mobiles where this simply does not occur, like mine for example :( , no magic needs to be done, just follow what the documentation says and assume it's correct.

I hope this helps, regards.