我用的是android和pubnub。
我做了订阅/发布教程,它工作,我有一个工作聊天(当应用程序是开放的)。
在阅读了所有教程之后,我了解到,当应用程序处于后台或关闭时,publish方法应该后退,向所有subscribe和enablePushNotificationsOnChannel用户发送推送通知。
因为某些原因,它对我不起作用,我也不知道它应该如何工作。
在本教程中,应该有一段代码:
网站:
/ Send Push Notification to all devices
// registered to `my_channel`
JSONObject jso = null;
try {
jso = new JSONObject("{
'aps' : {
'alert' : 'You got your emails.'," + "
'badge' : 9,
'sound' : 'bingbong.aiff'}," + "
'acme 1': 42
}");
pubnub.publish("my_channel", jso,
new Callback(){
@Override
public void successCallback(String arg0,
Object arg1) {
System.out.println(arg1);
}和And:
Sending Notifications
Sending a notification requires creating a JSON object. It is then added to a PubNub GCM specific message (the message is formatted for you).
public void sendNotification() {
PnGcmMessage gcmMessage = new PnGcmMessage();
JSONObject jso = new JSONObject();
try {
jso.put("GCMSays", "hi");
} catch (JSONException e) { }
gcmMessage.setData(jso);
PnMessage message = new PnMessage(
pubnub,
"your channel name",
callback,
gcmMessage);
try {
message.publish();
} catch (PubnubException e) {
e.printStackTrace();
}
}
Note that we have to create the callback methods which will be fired when the message is published:
public static Callback callback = new Callback() {
@Override
public void successCallback(String channel, Object message) {
Log.i(TAG, "Success on Channel " + CHANNEL + " : " + message);
}
@Override
public void errorCallback(String channel, PubnubError error) {
Log.i(TAG, "Error On Channel " + CHANNEL + " : " + error);
}
};因此,很明显,这是两段不同的代码,没有一个包含了这两个选项(在app打开时的实时通道,在app关闭时推送通知)。
我需要一段代码:
发布于 2016-02-28 22:29:17
PubNub移动推送通知
发布服务器不知道或不关心设备上的订阅服务器是活动的(前台)还是非活动的(后台或不运行)。Publisher总是使用GCM (可能还有APNS)有效负载发布消息。
请参阅我的文章在一个API调用中向订阅者和移动推送通知服务发送API和GCM消息,作为关于PubNub推送通知工作方式的入门文章。
从这里开始,您可能会有更多的问题,但这将为您提供一个关于如何使用PubNub移动推送通知的良好开端。
https://stackoverflow.com/questions/35627584
复制相似问题