question

Kim Strasser avatar image
Kim Strasser asked

I don't receive my cloud push notifications. Cloud script failed

I use the following cloud script to send push notifications but I don't receive the push notifications on the client device. I have only tested it on my Android device.

There is something wrong with my cloud script because I get this information in Playstream:

Player executed Cloud Script
FAILED: Welcome() (revision 12)
Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "Welcome",
    "CloudScriptExecutionResult": {
        "FunctionName": "Welcome",
        "Revision": 12,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [
            {
                "Level": "Error",
                "Message": "PlayFab API request error",
                "Data": {
                    "api": "/Server/SendPushNotification",
                    "request": {
                        "Package": {
                            "GeneratePlayStreamEvent": true,
                            "Message": "Hello",
                            "Title": "Welcome message"
                        }
                    },
                    "result": null,
                    "apiError": {
                        "code": 400,
                        "status": "BadRequest",
                        "error": "InvalidParams",
                        "errorCode": 1000,
                        "errorMessage": "Invalid input parameters",
                        "errorHash": null,
                        "errorDetails": {
                            "Recipient": [
                                "The Recipient field is required."
                            ]
                        }
                    }
                }
            }
        ],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.0212264,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 52880,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 0,
        "Error": {
            "Error": "CloudScriptAPIRequestError",
            "Message": "The script called a PlayFab API, which returned an error. See the Error logs for details.",
            "StackTrace": "Error\n    at handlers.Welcome (BFD0A-main.js:3:16)"
        }
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "F93550EA805475D8",
    "EventId": "97728e4c6325414cbcba0e1d3b2bb560",
    "SourceType": "BackEnd",
    "Timestamp": "2019-07-24T13:28:49.7759933Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "663385c"
    }
}

My cloud script code (revision 12):

handlers.Welcome = function (args, context)
{
    server.SendPushNotification({ Package : { GeneratePlayStreamEvent : true, Message : "Hello", Title: "Welcome message" }});
    log.info("This is a log statement!");
    log.debug("This is a debug statement.");
    log.error("This is... an error statement?");
}

In addition, I get this log in my client code in the SendPushNotification result. but I don't know what I'm doing wrong. What is wrong with my code?

Client code:

string Playfaberrormsg = "";

private async Task SendPushNotification()
{
  var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
  {
    FunctionName = "Welcome",
    GeneratePlayStreamEvent = true
  });

  if (result.Error != null)
  {
    Playfaberrormsg = result.Error.Error.ToString();          
  }
  else
  {
    Playfaberrormsg = "Sending Push notification successful";
  }
}

public async Task DeviceTokenAsync()
{
  await GetDeviceTokenAsync();
}

string Errormsg = "";

private async Task GetDeviceTokenAsync()
{
  ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
  string deviceToken = prefs.GetString("my_token", "");

  if (deviceToken != null)
  {
    var result = await PlayFabClientAPI.AndroidDevicePushNotificationRegistrationAsync(new AndroidDevicePushNotificationRegistrationRequest()
    {
      DeviceToken = deviceToken                    
    });

    if (result.Error != null)
    {
      Errormsg = result.Error.Error.ToString();
    }
    else
    {
      Errormsg = "Push Registration Successful";
      await SendPushNotification();
    }
  }
  else
    Errormsg = "Push Token was null!";
}
Push Notifications
pict1.jpg (450.6 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Seth Du avatar image
Seth Du answered

If you are implementing iOS push notification, have you register your client via RegisterForIOSPushNotification API call? For additional information, you may refer to my reply in this thread: https://community.playfab.com/questions/31348/how-can-i-send-and-receive-push-notifications.html.

6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kim Strasser avatar image Kim Strasser commented ·

Yes I did. I added my PlayFab registration code now to my question. The registration code was already in my project before I asked this question. Is it possible that something is wrong with my cloud script code?

I made another test with my Android project on my Android device and here the same issue. I don't receive the push notification on my Android device. But in PlayStream Monitor, the player registration seems to be successful: -Player executed Cloud Script SUCCESS: Welcome() (revision 8), -Player registered push notifications GoogleCloudMessaging.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

My suggestion here is first to figure out where the issue is. Try to manually call SendPushNotification in some RESTful API testing tool like Postman.

If it failed, there can be something wrong with the client and if it works, it means to be Cloud Scripts issue (you should also check the configuration in the game manager if cloud script function is triggered in Rules).

Besides, you may try the basic notification, which is the Message property in the request of SendPushNotification. After it works, you may test Package and AdvancedPlatformDelivery. There will be updates on Push notification system even though I cannot tell the ETA and as far as I know, some methods will cause that you will only receive data in onReceive function while it will not automatically pop up a notification.

Please feel free to reply this thread if you have any updates.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Seth Du ♦ commented ·

I receive the push notifications in my iOS and Android applications on real devices if I use CALL API here: https://api.playfab.com/documentation/server/method/SendPushNotification

How can I test if my cloud script code is correct? How can I find out if the cloud script function is triggered in Rules?

I'm not sure if I use the correct DeviceToken format in RegisterForIOSPushNotificationRequest and AndroidDevicePushNotificationRegistrationRequest. Are my tokens in the correct format so that I can use them with the API requests?

My Android token: fbb8hSA0GoY:APA91bGbK6yNErVm33u6uwzpjEGqm4x9Vqd0jLytMX5_3YWZ5gwBfnojHmA9IxqavghnNsGKJBi-KM5_TXf9o4oWyznFWWgZnFjpjqkmIlwvZtQbtuh3DF6lf7PivTpXt5zFBVoHpn9G

My iOS token: d1e22467 876634d8 3599c40a 93fc9fc8 e787e39a 560ec5c0 412b723e 54fa61a5

0 Likes 0 ·
Show more comments
Kim Strasser avatar image Kim Strasser commented ·

I found out how to add a recipient to my cloud script code. This code works on iOS and Android. I receive the push notifications on my iOS and Android device.

Cloud script:

handlers.Welcome = function (args, context)
{
    var targetId = args.TargetId;
    server.SendPushNotification({ Recipient : targetId, Package : { GeneratePlayStreamEvent : true, Message : "Hello", Title: "Welcome message" }});
    log.info("This is a log statement!");
    log.debug("This is a debug statement.");
    log.error("This is... an error statement?");
}

Client application:

string Playfaberrormsg = "", PlayerPlayFabID = "Get the PlayFabID";
private async Task SendPushNotification() { var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest() { FunctionName = "Welcome", FunctionParameter = new Dictionary<string, object>() { { "TargetId", PlayerPlayFabID } }, GeneratePlayStreamEvent = true }); if (result.Error != null) { Playfaberrormsg = result.Error.Error.ToString(); } else { Playfaberrormsg = "Sending Push notification successful"; } }
0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

The recipient is required in the SendPushNotification API, errors will occur without it.

Anyway, glad to hear that, any issues let us know, thanks:)

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.