question

Kim Strasser avatar image
Kim Strasser asked

What can I do when I get the error "PushNotEnabledForAccount" in Cloud Script?

I get this error message in Cloud Script and the rest of the code in the function GrantLeaderboardRewardsToAllPlayersFourthPart is not executed because of this error:

{
        "Message": "PlayFab API request error",
        "Level": "Error",
        "Data": {
          "request": {
            "Message": "28/02/2020 Leaderboard 1 Results Get the fantastic Bamboo Sword!",
            "Recipient": "989FAED7FD9ABBB2"
          },
          "api": "/Server/SendPushNotification",
          "apiError": {
            "errorDetails": null,
            "errorMessage": "PushNotEnabledForAccount",
            "errorHash": null,
            "errorCode": 1094,
            "status": "BadRequest",
            "error": "PushNotEnabledForAccount",
            "code": 400
          },
          "result": null
        }
      }
handlers.GrantLeaderboardRewardsToAllPlayersFourthPart = function (args, context)
{
  var notificationsend = false;
  var sendmessage = "test";
  var result = SendPushNotificationMessage(sendmessage);
        
  if (result == true)
      notificationsend = true;
  else
      notificationsend = false; 

  log.info("notificationsend: " + notificationsend);

  var resultevent = server.WritePlayerEvent({
                                      PlayFabId: currentPlayerId,
                                      EventName: "GrantLeaderboardRewardsToAllPlayersFourthPart_Finished",
                                      Body: {                                   
                                      "PlayFabID": currentPlayerId,
                                      "NotificationSend": notificationsend,
                                      "Sendmessage": sendmessage
                                      }
                                      });       
                                      
  if (resultevent.Error == null)
      log.info("Writing GrantLeaderboardRewardsToAllPlayersFourthPart_Finished event successful.");
  else
      log.info("Writing GrantLeaderboardRewardsToAllPlayersFourthPart_Finished event not successful.");
    
  return;
}

function SendPushNotificationMessage(message)
{
  log.info("PushMessage: " + message);
  var result = server.SendPushNotification({
           Recipient: currentPlayerId,
           Message: message
      });
        
  if ((result != null) && (result.Error == null))
      return true;
  else
      return false;
}

The function GrantLeaderboardRewardsToAllPlayersFourthPart is only triggered by a rule.

What can I do if a player has not yet enabled push notifications in his account? I want to continue executing the rest of the code in GrantLeaderboardRewardsToAllPlayersFourthPart.

I don't want that my entire function fails because there is some code after var result = SendPushNotificationMessage(sendmessage); that needs to be executed, even if it is not possible to send the push notification to the player. I just want to set notificationsend = false; if it is not possible to send the push notification and I want to log the error code so that I can store it with API server.WritePlayerEvent.

How can I avoid that my cloud script code fails?

CloudScript
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

·
Citrus Yan avatar image
Citrus Yan answered

In your case, what you need is the try-catch statement which handles exceptions and allows the remaining code to execute. Please navigate to this doc to learn more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

You can modify your SendPushNotificationMessage function to the following to handle errors:

function SendPushNotificationMessage(message)
{
  log.info("PushMessage: " + message);
  try{
        var result = server.SendPushNotification({
           Recipient: currentPlayerId,
           Message: message
      });
        
  }catch(e){
      log.error(e);
  }


  if ((result != null) && (result.Error == null))
      return true;
  else
      return false;
}


10 |1200

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

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.