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!"; }