I am making a game on android and ios and use firebase cloud messages with firebase functions to implement in app messaging between users. I receive those messages in unity through OnMessageReceived callback and everything works as intended on Android, but not on ios.
Here is my code sample on backend, when I send message through Firebase Messaging in Functions:
`
const message={
data:{
code:code.toString(),
data:undefined
}
}
if(data!=null)
{
if(typeof data !=='string')
{
data=JSON.stringify(data);
}
message.data.data=data;
}
await messaging.sendToDevice(tokens,message).
catch((err:any)=>{
error(err);
return -1;
});`
And here is code sample from client app, where I register for incoming messages and retrieve a device token, which I then send via function to database:
public void Init()
{
FirebaseMessaging.TokenRegistrationOnInitEnabled = true;
FirebaseMessaging.TokenReceived += OnTokenReceived;
FirebaseMessaging.MessageReceived += OnMessageReceived;
}
void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
GCMcallback?.Invoke(e.Message.Data);
}
public void TurnGcmOff()
{
FirebaseMessaging.MessageReceived -= OnMessageReceived;
FirebaseMessaging.TokenReceived -= OnTokenReceived;
gcmToken = null;
}
public async UniTaskVoid CheckGCMActivity()
{
if (!subscribed)
{
Debug.Log("Gcm activity is down. Re-subscribing");
await SubscribeToGCM();
}
}
public async UniTask SubscribeToGCM()
{
if(subscribed)
return;
subscribed = true;
if (!string.IsNullOrEmpty(gcmToken))
{
await SendGCMToken();
return;
}
for (int i = 0; i < tokenGetRetries; i++)
{
//Debug.Log("Getting token async");
var token =await FirebaseMessaging.GetTokenAsync();
if (!string.IsNullOrEmpty(token) && token != "StubToken")
{
Debug.Log($"Token received: {gcmToken}");
gcmToken = token;
}
//Debug.Log($"token loaded: {token}");
if (!string.IsNullOrEmpty(gcmToken)&&gcmToken != "StubToken")
{
TokenReceivedCallback?.Invoke(gcmToken);
await SendGCMToken();
return;
}
await UniTask.Delay(1000);
}
Debug.LogWarning("Not able to get token!");
}
OnMessageReceived callback doesnt fire on ios no matter what I do. Device token is actually generated on ios device and sent to database. APNS is uploaded to firebase console and Push Notifications capabilities added in XCode. I am actually able to receive background push notifications in ios, but not regular data messages in foreground. Just like Apple do not delivers these messages, if I do not include "notification" field in message payload. And I do not want to use push notifications overall, only custom data messages in foreground, which works perfect on android.
My Firebase Unity SDK version is 10.0.1, Unity version 2020.3.43.f1 and target OS level is iOS 13
2
Answers
I was eventually able to solve the problem. Seems that
was actually deprecated method, which still worked with android messages and notifications, and also with ios push notifications, but not with ios foreground messages. Documentation lacked this clarification, so it was purely luck finding out the issue. So I now use either
and everything work as expected. Note that in these new methods token should be included in message data like this
Hope that helps if someone faces this problem
If you’re experiencing issues with receiving Firebase Cloud Messages (FCM) without notifications on iOS in a Unity game while the app is in the foreground, there are a few things you can check and try to resolve the problem:
Verify Firebase Cloud Messaging setup: Ensure that you have properly integrated Firebase Cloud Messaging into your Unity game project. Make sure you have followed the Firebase documentation and added the necessary configuration files, dependencies, and code for FCM initialization.
Check Firebase Cloud Messaging capabilities: In your Xcode project, go to the Capabilities tab and ensure that the "Push Notifications" capability is enabled. This is required to receive FCM messages in the foreground on iOS.
Implement Firebase messaging delegate methods: In your Unity code, make sure you have implemented the necessary Firebase messaging delegate methods to receive FCM messages. The main methods to implement are DidReceiveMessage and DidReceiveRemoteNotification. These methods will be called when a message is received, even in the foreground.
Debug logging: Add debug logs in your Unity code to check if the Firebase messaging delegate methods are being called correctly. This will help you verify if the FCM messages are being received by your Unity game.
Check iOS app permissions: Ensure that the necessary permissions are correctly configured in your iOS app. Specifically, verify that you have requested and obtained the user’s permission to receive remote notifications by using the UserNotifications framework in your Unity code.
Test on a real device: Test the FCM functionality on a physical iOS device rather than using a simulator. Sometimes, certain features may not work as expected in simulators.
Review Apple’s guidelines: Make sure your implementation aligns with Apple’s guidelines for receiving remote notifications while the app is in the foreground. Apple has specific guidelines for handling notifications and background execution, so reviewing these guidelines can help ensure your implementation is correct.
If you have checked all these steps and are still facing issues, it may be helpful to provide more specific information about your implementation, error messages, or any relevant code snippets. That way, I can provide more targeted assistance.