firebase / quickstart-unity

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

OnMessageReceived not called sometimes when a cloud message with data sent #662

Closed emaadgh closed 4 years ago

emaadgh commented 4 years ago

Please fill in the following fields:

Unity editor version: 2019.1.10f1 Firebase Unity SDK version: firebase_unity_sdk_6.13.0 Source you installed the SDK (.unitypackage or Unity Package Manager): https://firebase.google.com/download/unity?authuser=1 Firebase plugins in use (Auth, Database, etc.): Analytics, Remote Config, InstanceId, Messaging Additional SDKs you are using (Facebook, AdMob, etc.): Platform you are using the Unity editor on (Mac, Windows, or Linux): Windows Platform you are targeting (iOS, Android, and/or desktop): Android Scripting Runtime (Mono, and/or IL2CPP): Mono

Please describe the issue here:

As described here I import FirebaseMessaging.unitypackage to my unity project like other Firebase plugins. Init the Firebase :

Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
  var dependencyStatus = task.Result;
  if (dependencyStatus == Firebase.DependencyStatus.Available) {

  } else {
    UnityEngine.Debug.LogError(System.String.Format(
      "Could not resolve all Firebase dependencies: {0}", dependencyStatus));

  }
});

Then add :

Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;

and their methods :

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

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

Also I'm using com.google.firebase.MessagingUnityPlayerActivity as my man activities name in AndroidManifest as described in the Docs.

My issue is when I send test messages with data from Firebase panel (Cloud messaging - Compose notification), I ALWAYS receive the notification (In the tray bar of the device) but sometimes when I tap on the notification, the OnMessageReceived callback will not be called, and that means the data not received in the client.

This is not just from test messages and I am getting the same result from scheduled messages like my game users. (not receiving notification's data ).

I tried to trace and find the issue but it seems this is something internal for FirebaseMessaging because in equal conditions I get sometimes notification and the data, and sometimes just the notification.

I also had this issue in firebase_unity_sdk_5.4.4 version and after update to the last version of firebase, the issue still persists.

The other thing is when the app is in the foreground, the data will always be received from the notification. This issue occurs only when the app is closed or is in the background.

google-oss-bot commented 4 years ago

This issue does not seem to follow the issue template. Make sure you provide all the required information.

alexames commented 4 years ago

Hi, I want to apologize for the delayed reply.

If I understand correctly, after you send a test message from Firebase console, your Android devices received a notification in the System Tray. However, when you tap on the notification, the app will be launched but OnMessageReceived callback is not triggered. This only happened when the app is closed or in the background. And the issue persists from Unity 5.4.4 to 6.13.0.

Does that capture your issue correctly?

If that is the case, please take a look of this doc https://firebase.google.com/docs/cloud-messaging/unity/client?authuser=1#note_about_message_delivery_on_android https://firebase.google.com/docs/cloud-messaging/unity/client?authuser=1#configuring_a_custom_entry_point_activity

Messages can have two kinds of information that hands off of them, the Notification and the Data. If the app is in the foreground, your app will always receive them. But if your app is in the background, there are some caveats to what data will be received and how.

Using the MessagingUnityPlayerActivity instead of the default one should ensure that your message data reaches the callbacks that you've assigned. If you are not receiving the data, double check that the manifest file is set up correctly. If it's the Notification you're not receiving while your app is in the background, that's expected. If you need the data stored in the notification in your app, you can work around this by duplicating the information you need in the message Data.

I hope that helps. Let me know if you continue to have issues.

emaadgh commented 4 years ago

Hi, Thanks for your answer. Yes, you described my issue correctly.

I double-checked the manifest file and here it is :

<?xml version="1.0" encoding="UTF-8"?>
<manifest 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" 
 package="com.unity3d.player" 
 android:installLocation="preferExternal">
   <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />

   <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:isGame="true" android:usesCleartextTraffic="true">
     <meta-data
      android:name="com.google.android.gms.ads.AD_MANAGER_APP"
      android:value="true" />

     <!-- The MessagingUnityPlayerActivity is a class that extends
         UnityPlayerActivity to work around a known issue when receiving
         notification data payloads in the background. -->
     <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
         <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
       </intent-filter>
       <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <category android:name="android.intent.category.BROWSABLE" />
         <!-- Accepts URIs that begin with "example://gizmos” -->
         <data android:scheme="app"
               android:host="open.companyname.appname" />
       </intent-filter>
       <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
     </activity>
     <service android:name="com.google.firebase.messaging.MessageForwardingService" android:exported="false" />

     <activity android:name="tapsell.sdk.TapsellAdActivity" android:configChanges="keyboardHidden|orientation|screenSize" />
     <activity
       android:name="com.chartboost.sdk.CBImpressionActivity"
       android:configChanges="keyboardHidden|orientation|screenSize"
       android:excludeFromRecents="true"
       android:hardwareAccelerated="true"
       android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

     <meta-data android:name="billing.service" android:value="bazaar.BazaarIabService" />
     <activity android:name="com.bazaar.BazaarIABProxyActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

      <meta-data android:name="unity.build-id" android:value="633a700a-7389-4a19-972c-ac1f177788f8" />
      <meta-data android:name="unity.splash-mode" android:value="0" />
      <meta-data android:name="unity.splash-enable" android:value="True" />
      <meta-data android:name="android.max_aspect" android:value="1.86" />
   </application>   
   <uses-feature android:glEsVersion="0x00020000" />   
   <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
   <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
   <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

Is any thing wrong in this manifest?

About the notification and data, you are right. I'm not receiving the data part in callback sometimes and that's my issue. This data can be set in the panel like this :

firebase

Thanks.

emaadgh commented 4 years ago

Hi @alexames , any update on this issue?

alexames commented 4 years ago

Sorry for the delay.

At first glance your manifest file looks correct. I'm not sure what could be causing the issue. Would you mind trying to build the quickstart testapp for messaging in this repo and then seeing if the problem persists in the testapp? If it doesn't, we can see what is different between the testapp and your project. If that doesn't work either, we'll have to dig some more.

google-oss-bot commented 4 years ago

Hey @emaadgh. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

emaadgh commented 4 years ago

Hi, I'm trying to test the scenario in the test app project and I will reply result here.

google-oss-bot commented 4 years ago

Hey @emaadgh. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 4 years ago

Since there haven't been any recent updates here, I am going to close this issue.

@emaadgh if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.