【发布时间】:2016-02-25 12:39:04
【问题描述】:
我正在使用带有 pubnub 的 android。
我完成了订阅/发布教程并且它有效,我有一个有效的聊天(当应用程序打开时)。
阅读所有教程后,我了解到当应用程序处于后台或关闭时,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);
}
还有:
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);
}
};
因此,很明显,这是两段不同的代码,它们都没有涵盖两个选项(应用打开时的实时通道,应用关闭时的推送通知)。
我需要一段代码:
- 如果应用程序打开 - 通过渠道发送数据。
- 如果应用程序已关闭/在后台 - 发送推送通知。
【问题讨论】:
标签: android push-notification google-cloud-messaging real-time pubnub