我正在使用PubNub API来获取实时消息。我已经实现了代码来订阅我的频道并接收正在进行的消息。我只是希望这些消息在后台接收,即使我的应用程序没有打开。我已经了解了android中的服务和广播接收器,但我不了解如何在PubNub中使用它。有没有正确的方法来实现这个目标?
PubNub订阅代码
public static final String GLOBAL_CHANNEL = "NAME_OF_MY_CHANNEL";
public void subscribe() {
Callback callback = new Callback() {
@Override
public void connectCallback(String channel, Object message) {
notifyUser("Subscribed to location updates");
}
@Override
public void disconnectCallback(String channel, Object message) {
notifyUser("Unsubscribed to location updates");
}
public void reconnectCallback(String channel, Object message) {
notifyUser("Resubscribed to location updates");
}
@Override
public void successCallback(String channel, final Object message) {
notifyUser(message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
notifyUser("Check your internet connection...!");
}
};
try {
pubnub.subscribe(GLOBAL_CHANNEL, callback);
} catch (PubnubException e) {
System.out.println(e.toString());
}
}
public void notifyUser(String message) {
final String msg = (String) message;
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, msg, 0).show();
}
});
}
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以使用PubNub subscribe-a-boot sample app开始使用。如果您对实施此问题有任何疑问,请联系support@pubnub.com。