Android Notifications不显示邮件正文

时间:2017-04-18 16:02:18

标签: android delphi google-cloud-messaging

我正在编写自己的消息提供程序,以便在iOS和Android上发送推送通知。我让提供商在iOS上运作良好。我的问题是Android。在应用程序未运行的情况下,手机会收到消息并通知用户有消息进来。我只看到消息中的图标。邮件正文不会显示。如果应用程序正在运行,应用程序将获取消息事件,我可以在JSON中看到消息正文和消息标题。所以看来信息正在传递。我在运行JellyBean和Marshmallow的Android上试过这个并得到相同的结果。我也尝试从他们的网站上使用谷歌测试通知进行FCM。我的网络通知发送得到相同的结果。有什么想法吗?

以下是我在Delphi中使用的代码:

HttpClient.Request.URL := 'https://fcm.googleapis.com/fcm/send';

HttpClient.Request.ContentType := 'application/json';
HttpClient.Request.CustomHeaders.Add('Authorization: key = ***'); // Server Key from Google
HttpClient.Request.CharSet := 'utf-8';

JSONMsg := TJSONObject.Create;
JSONMsg.AddPair('to', devicetoken);

JSONInfo := TJSONObject.Create;
JSONInfo.AddPair('body', edtMessage.Text);
JSONInfo.AddPair('title', 'CODY Mobility');
JSONInfo.AddPair('priority', 'high');
JSONTrue := TJsonTrue.Create;
JSONFalse := TJsonFalse.Create;
JSONInfo.AddPair('content_available', JSONTrue);
JSONInfo.AddPair('dry_run', JSONFalse);

JSONToSend := TStringStream.Create(JSONMsg.ToString, TEncoding.UTF8);

StatusMemo.Lines.Add('Sending Android message to device: ' + deviceToken);
try
    HttpClient.Post('https://fcm.googleapis.com/fcm/send', JSONToSend);
except
    on E:Exception do begin
        StatusMemo.Lines.Add('Message send failed: ' + E.Message);
    end;
end;

StatusMemo.Lines.Add('Android message response: ' + HttpClient.ResponseText);
FreeAndNil(JSONTrue);
FreeAndNil(JSONFalse);

1 个答案:

答案 0 :(得分:0)

我发现了我的问题!它似乎是Delphi的一个错误。我发送的原始邮件是:

{"to":<MyDeviceID>","notification":{"body":"It finally works!!!!!","title":"CODY Mobility Title","priority":"high","content_available":false,"dry_run":false}}

如果我将消息更改为以下内容,它现在可以正常运行:

{"to":"<MyDeviceID>","data":{"message":"It finally works!!!!!","title":"CODY Mobility Title","priority":"high","content_available":false,"dry_run":false}}

我不得不改变&#34;通知&#34;到&#34;数据&#34;我不得不改变身体&#34;元素到&#34;消息&#34;元件。有了这些更改,手机会在应用关闭时显示消息并显示消息。当然,这不再符合Google的规范,即通过Firebase Cloud Messaging发送通知。我挖掘了Delphi代码,发现了一个名为NotificationPublisher.Java的Delphi java脚本。在那里,有代码专门查找消息元素。

 if (jsonVal != null) {
if (jsonVal.has("message"))
{ msg = jsonVal.optString("message"); }

else if (jsonVal.has("msg"))
{ msg = jsonVal.optString("msg"); }

else if (jsonVal.has("alert"))
{ msg = jsonVal.optString("alert"); }

if (jsonVal.has("title"))
{ title = jsonVal.optString("title"); }

}
} else {
// Look for msg or message in bundle
if (key.equals("message"))
{ msg = valstr; } else if (key.equals("msg")) { msg = valstr; }

if (key.equals("title"))
{ title = valstr; }
}

我不确定Delphi在哪里检查Notification元素与Data元素。但我相信这是问题的一部分。我创建了一张支持票,因此Embarcadero可以进一步研究这个问题。如果其他人有这个问题......只需更改您的信息即可。