esl / MongoosePush

MongoosePush is a simple Elixir RESTful service allowing to send push notification via FCM and/or APNS.
Apache License 2.0
108 stars 24 forks source link

FCM error: Unable to complete push request due to service error: INVALID_ARGUMENT in category: invalid_request #123

Closed theanhoo closed 4 years ago

theanhoo commented 4 years ago

MongoosePush version: mongoose_push-2.0.0-alpha.4

docker pull mongooseim/mongoose-push

on 24 November 2019

Request:

2019-11-24T12:50:53.912 [debug] pid=<0.1873.0> action=push_fcm_notification, request=%Sparrow.H2Worker.Request{body: 
  "{\"message\":{\"android\":{\"data\":{\"last-message-body\":\"1250\",\"last-message-sender\":\"user@example.com\",\"message-count\":1},\"notification\":{},\"priority\":\"HIGH\"},\"data\":{},\"notification\":{},\"token\":\"secret-token\"}}", 
  headers: [{"content-type", "application/json"}], 
  path: "/v1/projects//messages:send", 
  timeout: 5000}

Response from FCM (error):

{
  "error": {
    "code": 400,
    "message": "Invalid value at 'message.android.data[2].value' (TYPE_STRING), 1",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "message.android.data[2].value",
            "description": "Invalid value at 'message.android.data[2].value' (TYPE_STRING), 1"
          }
        ]
      }
    ]
  }
}

Please help.

Many thanks in advance.

rslota commented 4 years ago

Hi @theanhoo ,

Apparantly FCMv1 does not tolerate values that are not a string within data payload. I'm adding bugfix task to esl/sparrow backlog. Until this is resolved please pass all values as strings to MongoosePush API - here the problem is specifically with: "message-count": 1.

theanhoo commented 4 years ago

Hi, @rslota,

I've removed binary_to_integer from the following function in MongooseIM:

%% Create notification for API v2
make_notification(v2, Notification, Options = #{<<"silent">> := <<"true">>}) ->
    %% MessageCount = binary_to_integer(maps:get(<<"message-count">>, Notification)),
    MessageCount = maps:get(<<"message-count">>, Notification),
    {ok, #{
        service => maps:get(<<"service">>, Options),
        mode => maps:get(<<"mode">>, Options, <<"prod">>),
        topic => maps:get(<<"topic">>, Options, null),
        priority => "high",
        data => Notification#{<<"message-count">> => MessageCount}
    }};
make_notification(v2, Notification, Options) ->
    {ok, #{
        service => maps:get(<<"service">>, Options),
        mode => maps:get(<<"mode">>, Options, <<"prod">>),
        alert => #{
            body => maps:get(<<"last-message-body">>, Notification),
            title => maps:get(<<"last-message-sender">>, Notification),
            tag => maps:get(<<"last-message-sender">>, Notification),
            badge => binary_to_integer(maps:get(<<"message-count">>, Notification)),
            click_action => maps:get(<<"click_action">>, Options, null)
        },
        topic => maps:get(<<"topic">>, Options, null)
    }}.

This is the request:

{
    "message": {
        "android": {
            "data": {
                "last-message-body": "6",
                "last-message-sender": "user@example.com",
                "message-count": "1"
            },
            "notification": {},
            "priority": "HIGH"
        },
        "data": {},
        "notification": {},
        "token": "XXX"
    }
}

And this is the response:

2019-11-28T16:55:39.082 [debug] pid=<0.1820.0> action=send, item=request_response, to={#PID<0.1971.0>, #Reference<0.733623136.701235201.235185>}, headers=[{":status", "400"}, {"vary", "X-Origin"}, {"vary", "Referer"}, {"vary", "Origin,Accept-Encoding"}, {"content-type", "application/json; charset=UTF-8"}, {"date", "Thu, 28 Nov 2019 08:55:39 GMT"}, {"server", "ESF"}, {"cache-control", "private"}, {"x-xss-protection", "0"}, {"x-frame-options", "SAMEORIGIN"}, {"x-content-type-options", "nosniff"}, {"alt-svc", "quic=\":443\"; ma=2592000; v=\"46,43\",h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000"}, {"accept-ranges", "none"}], body={
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT"
  }
}

Please advise.

Many thanks in advance.

theanhoo commented 4 years ago

I have switched back to MongoosePush 1.0.6. That seems to work fine.