question

Takuya Nakajo avatar image
Takuya Nakajo asked

The first of AdvancedPlatformDelivery always runs

Hello.

I'm writing SendPushNotification as follows.

[FunctionName("SendPushNotification")]
public static async Task<dynamic> SendPushNotification(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
{
    string body = await req.ReadAsStringAsync();
    var context = JsonConvert.DeserializeObject<PlayerPlayStreamFunctionExecutionContext<dynamic>>(body);
    string playFabId = context.PlayerProfile.PlayerId;

    var serverApi = GetServerInstance(context.TitleAuthenticationContext);
    var result = await serverApi.SendPushNotificationAsync(new SendPushNotificationRequest
    {
        Recipient = playFabId,
        AdvancedPlatformDelivery = new List<AdvancedPushPlatformMsg>
        {
            new AdvancedPushPlatformMsg
            {
                Json = "{\"alert\":{\"title\":\"プッシュ通知タイトル\",\"body\":\"プッシュ通知メッセージ\",\"badge\":1,\"sound\":\"default\"}",
                Platform = PushNotificationPlatform.ApplePushNotificationService
            },
            new AdvancedPushPlatformMsg
            {
                Json = "{\"title\":\"プッシュ通知タイトル\",\"body\":\"プッシュ通知メッセージ\",\"sound\":\"default\"}",
                Platform = PushNotificationPlatform.GoogleCloudMessaging
            },
        }
    });

    return new
    {
        error = PlayFabSimpleJson.SerializeObject(result.Error),
        result = PlayFabSimpleJson.SerializeObject(result.Result)
    };
}

private static PlayFabServerInstanceAPI GetServerInstance(TitleAuthenticationContext titleAuthenticationContext)
{
    var apiSettings = new PlayFabApiSettings
    {
        TitleId = titleAuthenticationContext.Id,
        DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process),
    };

    return new PlayFabServerInstanceAPI(apiSettings);
}

I have specified two in AdvancedPlatformDelivery, iOS and Android, but apparently the first one is being used in a fixed way.

This means that when I put iOS and Android in the list in order, when I run Android push notifications, the iOS one will be called and I will get an error.

"error": "{\"HttpCode\":400,\"HttpStatus\":\"BadRequest\",\"Error\":1071,\"ErrorMessage\":\"Invalid JSON in request\",\"ErrorDetails\":{\"ValidationError\":[\"Unexpected end when deserializing object. Path 'alert', line 1, position 80.\"]},\"RequestId\":\"Failed to Enumerate RequestId. Exception message: Enumeration has not started. Call MoveNext.\"}",

Is this a bug?

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

·
JayZuo avatar image
JayZuo answered

As the error message said, you are providing a wrong JSON string in your AdvancedPushPlatformMsg. If you unescape your JSON string

{\"alert\":{\"title\":\"プッシュ通知タイトル\",\"body\":\"プッシュ通知メッセージ\",\"badge\":1,\"sound\":\"default\"}

You will find it misses a "}".

{"alert":{"title":"プッシュ通知タイトル","body":"プッシュ通知メッセージ","badge":1,"sound":"default"}

You can add the "}" and use the following string to see if it works:

{\"alert\":{\"title\":\"プッシュ通知タイトル\",\"body\":\"プッシュ通知メッセージ\"},\"badge\":1,\"sound\":\"default\"}
1 comment
10 |1200

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

Takuya Nakajo avatar image Takuya Nakajo commented ·

I missed it. I'm sorry. It worked well.Thank you.

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.