Android pubnub-发布回退以推送通知';行不通
我正在使用带有pubnub的android 我做了订阅/发布教程,它工作了,我有一个工作聊天(当应用程序打开时) 阅读了所有教程后,我了解到,当应用程序处于后台或关闭状态时,Android pubnub-发布回退以推送通知';行不通,android,push-notification,google-cloud-messaging,real-time,pubnub,Android,Push Notification,Google Cloud Messaging,Real Time,Pubnub,我正在使用带有pubnub的android 我做了订阅/发布教程,它工作了,我有一个工作聊天(当应用程序打开时) 阅读了所有教程后,我了解到,当应用程序处于后台或关闭状态时,publish方法应该回退,向subscribe和enablePushNotificationsOnChannel订阅频道的所有人发送推送通知 出于某种原因,它对我不起作用,我也不确定它应该如何起作用 从教程中,应该有一段代码: / Send Push Notification to all devices // regis
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);
}
};
/ 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);
}
};
以及:
/ 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);
}
};
所以,很明显,这是两段不同的代码,它们都没有涵盖这两个选项(应用程序打开时的实时频道,应用程序关闭时的推送通知)
我需要一段代码:
- 活动应用程序将同时接收这两个消息,并阻止通过操作系统的推送消息接收器侦听器显示推送通知
- 非活动应用程序将只接收推送通知并显示该通知。当最终用户点击推送消息时,它将打开应用程序,您可以在其中获取错过的消息,并在应用程序的UI中显示完整内容