Android pubnub-发布回退以推送通知';行不通

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

我正在使用带有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);
    }
};
  • 通过实时通道发送数据
  • 如果应用程序已关闭/处于后台,则发送推送通知
  • 来自网站:

    / 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);
        }
    };
    
    所以,很明显,这是两段不同的代码,它们都没有涵盖这两个选项(应用程序打开时的实时频道,应用程序关闭时的推送通知)

    我需要一段代码:

  • 如果应用程序已打开-通过通道发送数据
  • 如果应用程序关闭/在后台-发送推送通知
  • PubNub移动推送通知 发布者不知道或不关心订阅服务器在设备上是活动(前台)还是非活动(后台或根本不运行-终止状态)。Publisher始终使用GCM(可能还有APNS)负载发布消息

    • 活动应用程序将同时接收这两个消息,并阻止通过操作系统的推送消息接收器侦听器显示推送通知
    • 非活动应用程序将只接收推送通知并显示该通知。当最终用户点击推送消息时,它将打开应用程序,您可以在其中获取错过的消息,并在应用程序的UI中显示完整内容
    请参阅我的文章,作为PubNub推送通知工作原理的入门

    从这里开始,您可能会有更多的问题,但这将为您提供一个关于如何使用PubNub移动推送通知的良好开端