Java 如何使用Pubnub在Android中发送FCM通知?
我是pubnub的新手。我必须使用Pubnub服务发送FCM通知。我正在用这个 我已经创建了一个FCM服务器密钥,并在Pubnub仪表板上添加了该密钥。还创建了移动设备上的设备令牌。然后按如下方式在通道上启用PushNotificationsJava 如何使用Pubnub在Android中发送FCM通知?,java,android,google-cloud-messaging,firebase-cloud-messaging,pubnub,Java,Android,Google Cloud Messaging,Firebase Cloud Messaging,Pubnub,我是pubnub的新手。我必须使用Pubnub服务发送FCM通知。我正在用这个 我已经创建了一个FCM服务器密钥,并在Pubnub仪表板上添加了该密钥。还创建了移动设备上的设备令牌。然后按如下方式在通道上启用PushNotifications pubNotification.addPushNotificationsOnChannels() .pushType(PNPushType.GCM) .channels(Arrays.asLi
pubNotification.addPushNotificationsOnChannels()
.pushType(PNPushType.GCM)
.channels(Arrays.asList(CHANNEL_NAME_NOTIFICATION))
.deviceId(deviceid)
.async(new PNCallback<PNPushAddChannelResult>() {
@Override
public void onResponse(PNPushAddChannelResult result, PNStatus status) {
// handle response.
}
});
pubNotification.AddPushNotificationsOnChannel()
.pushType(PNPushType.GCM)
.channels(数组.asList(通道名称通知))
.deviceId(deviceId)
.async(新的PNCallback(){
@凌驾
public void onResponse(PNPushAddChannelResult结果,PNStatus状态){
//处理响应。
}
});
然后像这样发布通知
JSONObject object = new JSONObject();
JSONObject parent = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("title","sandeep");
jsonObject.put("name","sandeep");
parent.put("data",jsonObject);
object.put("pn_gcm",parent);
} catch (JSONException e) {
e.printStackTrace();
}
pubNotification.publish()
.message(object)
.channel(CHANNEL_NAME_NOTIFICATION)
.async(new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
}
});
JSONObject object=new JSONObject();
JSONObject父对象=新的JSONObject();
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject.put(“标题”、“sandeep”);
jsonObject.put(“名称”、“sandeep”);
parent.put(“数据”,jsonObject);
object.put(“pn_gcm”,父对象);
}捕获(JSONException e){
e、 printStackTrace();
}
pubNotification.publish()
.消息(对象)
.频道(频道名称通知)
.async(新的PNCallback(){
@凌驾
public void onResponse(PNPublishResult结果、PNStatus状态){
}
});
然后我添加了
onMessageReceived()
方法来接收消息,但没有收到任何通知。有什么问题吗?有人能帮忙吗。你在哪里添加了onMessageReceived()
?它必须是GcmBroadcastReceiver
@CraigConover,我在-public类MyFirebaseMessagingService Extendes FirebaseMessagingService{}中添加了onMessageReceived()方法。您确定您的消息负载结构正确吗?我在代码中没有看到您在有效负载(object
)中使用的jsonObject
。@AL-Map-payload=new-HashMap();有效载荷。put(“pn_gcm”,对象);然后在.message()中添加了此有效负载。我也尝试过此操作。哦,对了。我的错在那一点上。您是否收到任何错误响应?