Sign In Register

How can we help you today?

Start a new topic
Answered

FCM Implementation in Unity

I've been trying to implement FCM with Unity and GameSparks.
Has anyone had sucess doing this?

Set the Listeners on Start

And then on 

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)

So I can send it to GameSpark with a PushRegistrationRequest
And after that I can cal the Msg Event from the tutorial.

But I'm not getting a Token when the App Starts.

Anyone managed to implement this?
Thanks in advance.


Best Answer

Hi Flávio 


To enable the default message sound add "sound" : true to the FCM template json in Messages

Please to go to configurator->messages->ScriptMessage Extensions and edit the script message you want to send as a notification.

Under FCM message Template 

{

  "notification": {

    "title": "${data.title}",

    "body": "${data.body}",

    "sound": true

  }

}  


Regards

Katie


*and then on OnTokenReceived, I store it on a .txt to test and send it to GS

I've managed to get the Token, but I'm still confused ...

I've got the following in my code

 

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
        tokenString.text = token.Token;

        Debug.Log("Received Registration Token: " + token.Token);
         new PushRegistrationRequest()
         .SetDeviceOS("FCM")
         .SetPushId(token.Token)
         .Send((response) => {
             string registrationId = response.RegistrationId;
             GSData scriptData = response.ScriptData;
         });

    }

 

I login on my device, and see the token in a label.
Then i go back to Harness and run the provided Event on tutorial 


 

var msg = Spark.message(null);
msg.setPlayerIds([Spark.getPlayer().getPlayerId()]);
msg.setMessageData({"title":"test title","body":"test body"});
msg.send();

To send a message, but i don't receive the message on device.

Sockets are, OFF Pussh is ON , Supress push is OFF so it always pushes.

I'm pretty sure I'm doing something wrong but don't know what.

var msgData = msg.Send();

 

{
  "@class": ".LogEventResponse",
  "scriptData": {
    "msgData": "undefined"
  }
}

 


I can send PushNotifications through Firebase Console, even when my application is Killed.
(I had a little trouble because my device is a Huawei Honor)


But still didn't managed to get it working on GameSparks.

My device wasn't receiving notifications because I had a button to do a LogEventRequest to message EVENT, If my application is running and focused I can't receive the notification.

However, with application in background or killed, I receive notifications just fine If I run the Event through the TestHarness.
No sound on notification though, Like through the FirebaseConsole, I'll have to do some reasearch on firebase docs.


Answer

Hi Flávio 


To enable the default message sound add "sound" : true to the FCM template json in Messages

Please to go to configurator->messages->ScriptMessage Extensions and edit the script message you want to send as a notification.

Under FCM message Template 

{

  "notification": {

    "title": "${data.title}",

    "body": "${data.body}",

    "sound": true

  }

}  


Regards

Katie

Done, Worked out of the box :)

Thank you Katie.

Hi Flávio


Great to hear it is working.

You can also add a custom notification sound

by changing true to a string of the sound file name(don't include file extension)

You will need to place the sound file in Unity under Assets\Plugins\Android\res\raw, you may need to create the res and raw folders

The app will need to be rebuilt for Device to get these changes.


Regards

Katie


1 person likes this

Definitely I'm gonna need this!! Thank you very much for the tip!

Hi Flávio

I have not had this problem, If a user has this disabled on there device I don't think there would be a way around it.
Push notifications are set to high by default but you can manual add it
{
  "priority": "high",
  "notification": {
  "title": "${data.title}",
  "body": "${data.body}",
  "sound": "mysound"
  }
}

Regards
Katie

 

I thought GameSparks would act like a service after building, and then, when user Receives Push Notification, It would call the App to run in background or something.

My problem is that by default In android systems, App Settings Notification Method => LockScreen is OFF, and Battery => Keep running after screen off is OFF aswell, so I can't receive Notifications.

It is most likely some user-permissions I have to set in Android Manifest and have nothing to do with GameSparks I think.

Just to clarify some users that might come across the same issue.


It seems that  Huawei has a built in feature called Protected apps that prevents applications from keep running after the screen is turned Off. And by default installed applications are not put on the Protected Apps list. It's a Power Management Feature.
(Galaxy S7 has something like that aswell I think)

So , it force closes the App, and users can't receive notifications.

There is no way to declare this on Android Manifest file.
Just have to Advice users in a FAQ.

Notifications are OK if I close the app through "Swipe" on Android Opened Applications Menu, because it doesn't force close.

Anyway, appart from this, everything is running properly.

Login to post a comment