我正在尝试使用FCM gem将推送通知消息发送到Android手机。我能够获得设备令牌,我想我已经按照应有的方式进行了设置。仅当我使用红宝石后端发送测试消息时,我才会收到以下响应:
{:body=>"{\"multicast_id\":7171621681139926581,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1556626239337337%e087ffcde087ffcd\"}]}", :headers=>{"content-type"=>["application/json; charset=UTF-8"], "date"=>["Tue, 30 Apr 2019 12:10:39 GMT"], "expires"=>["Tue, 30 Apr 2019 12:10:39 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alt-svc"=>["quic=\":443\"; ma=2592000; v=\"46,44,43,39\""], "accept-ranges"=>["none"], "vary"=>["Accept-Encoding"], "connection"=>["close"]}, :status_code=>200, :response=>"success", :canonical_ids=>[], :not_registered_ids=>[]}
但在电话上我没有收到通知。
我的红宝石后端看起来像这样:
fcm_client = FCM.new(server_key)
# options = {:notification => "Test notification",:data => {:message => "This is a FCM Topic Message!"}}
options = { "notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1"
}
}
user_device_ids.each_slice(20) do |device_ids|
response = fcm_client.send_notification(device_ids, options)
p response
end
当应用打开时,它将关闭,并且我发现此错误:
java.lang.RuntimeException:无法实例化服务
NAME_OF_THE_APP.java.MyFirebaseMessagingService:
java.lang.ClassNotFoundException:找不到类
“ NAME_OF_THE_APP.java.MyFirebaseMessagingService”
现在我不是Java专家,所以我不知道这有什么问题。我正在华为手机上对其进行测试。
最佳答案
已经发现我的错了。我遵循了有关Firebase云消息传递的指南,在我的Androidmanifest中,文档说您需要传递此信息:
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
但比起它给我一个错误,那就是找不到该类。稍作搜索后,如果发现android名称错误:
应该是:
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService">
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>