我正在尝试使用rest API从FCM向android客户端发送通知。我的代码是:``
post_offline_message(From, To, Body) ->
K = "FCM key",
U = "https://fcm.googleapis.com/fcm/send",
P = #{
payload => #{
to => <<"rid">>,
priority => <<"high">>,
data => #{
<<"title">> => <<"Some Title">>,
<<"message">> => <<"Hi">>
}
}
},
EP = jiffy:encode(P),
?INFO_MSG("EP data: ~p", [EP]),
httpc:set_options([{keep_alive_timeout, 0}]),
{_, Resp} = httpc:request(post, {U, [{"Authorization", "key=" ++ K}], "application/json", EP}, [], []),
?INFO_MSG("FCM response: ~p", [Resp]).
My EP data (payload) is this:
**<<"{"payload":{"to":"dOqZOggYQZG6xKVY9P4_Xi:APA91bG0kuM-o_lSf3fUaWcyiW0fVj8-L49QgzU6rWfxi3o5lMaKapkOjvvLUxm-e78XS49TVl5jjgQt6DrRKTaDK2xzg-ffm1Qe4Xx-61_Hrmr6I0cPOcGAZ9Wv7QgFFjGXtWwWEvSi","priority":"high","data":{"title":"Some Title","message":"Hi"}}}">>**
and response from FCM is:
**{{"HTTP/1.1",400,"Bad Request"},[{"cache-control","private, max-age=0"},{"date","Thu, 03 Sep 2020 08:57:56 GMT"},{"accept-ranges","none"},{"server","GSE"},{"vary","Accept-Encoding"},{"content-length","3"},{"content-type","text/plain; charset=UTF-8"},{"expires","Thu, 03 Sep 2020 08:57:56 GMT"},{"x-content-type-options","nosniff"},{"x-frame-options","SAMEORIGIN"},{"content-security-policy","frame-ancestors 'self'"},{"x-xss-protection","1; mode=block"},{"alt-svc","h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43""}],"ton"}**
Please suggest what I am doing wrong or missing something. I am calling this method from ejabberd hook which is **offline_message_hook**.
花了4个小时后,我发现了自己的错误。我只是从请求中删除了有效载荷标签,它对我有效。我更新的请求是:
post_offline_message(From, To, Body) ->
K = "FCM key",
U = "https://fcm.googleapis.com/fcm/send",
P = #{
to => <<"rid">>,
priority => <<"high">>,
data => #{
<<"title">> => <<"Some Title">>,
<<"message">> => <<"Hi">>
}
},
EP = jiffy:encode(P),
?INFO_MSG("EP data: ~p", [EP]),
httpc:set_options([{keep_alive_timeout, 0}]),
{_, Resp} = httpc:request(post, {U, [{"Authorization", "key=" ++ K}], "application/json", EP}, [], []),
?INFO_MSG("FCM response: ~p", [Resp]).