Android Pusher Singleton频道订阅

时间:2017-09-05 05:48:02

标签: android pusher

您好我已经实现了pusher实时聊天和订阅推送频道,但我有很多活动和片段,我想听听pushr事件。我在每个活动/片段中添加了此代码,但问题是它为每个id创建了多个订阅。我知道我必须使用Singleton这可以让任何人指出我正确的方向来实现这一目标吗?

以下是我在每个活动/片段中编写的代码

 private PusherOptions options;
 private Channel channel;
 private Pusher pusher;    
 options = new PusherOptions();
 options.setCluster("ap2");
 pusher = new Pusher("afbfc1f591fd7b70190f", options);
 pusher.connect();

     profile_id = Global.shared().preferences.getString("PROFILE_ID", " ");

    channel = pusher.subscribe(profile_id);
    channel.bind("message",
            new SubscriptionEventListener() {
                @Override
                public void onEvent(String s, String s1, final String data) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject result = new JSONObject(data);
                                String message = result.getString("message");
                                String time = result.getString("time");
                                String reId = result.getString("recieverId");
                                new_message = message;
                                getConvoData(k, message);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            System.out.println("DATA ====>>" + data);
                        }
                    });
                }
            });

2 个答案:

答案 0 :(得分:1)

好吧所以在尝试了一段时间之后我想出了我的自我,我创建了一个全局类,并且只是添加了推送代码,以便它只为应用程序的整个生命周期维护一个连接

public class Global extends MultiDexApplication {

    @Override
        public void onCreate() {
            super.onCreate();

            SharedPreferences preferences = sharedInstance.getSharedPreferences(sharedInstance.getString(R.string.shared_preferences), Context.MODE_PRIVATE);
            sharedInstance.preferences = preferences;
            connectTopusher();


        }

 public void connectTopusher() {
        PusherOptions options;
        Channel channel;
        Pusher pusher;
        options = new PusherOptions();
        options.setCluster("ap2");
        pusher = new Pusher("afbfc1f591fd7b70190f", options);
        pusher.connect();
        String profile = Global.shared().preferences.getString("PROFILE_ID", "");
        channel = pusher.subscribe(profile);

        channel.bind("message",
                new SubscriptionEventListener() {
                    @Override
                    public void onEvent(String s, String s1, final String data) {
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject result = new JSONObject(data);
                                    String message = result.getString("message");
                                    String time = result.getString("time");
                                    String reId = result.getString("recieverId");

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                System.out.println("DATA ====>>" + data);
                            }
                        });
                    }
                });


        channel.bind("status_change", new SubscriptionEventListener() {
            @Override
            public void onEvent(String s, String s1, final String data) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject result = new JSONObject(data);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        System.out.println("DATA ====>>" + data);
                    }
                });
            }


        });


    }

答案 1 :(得分:0)

您可以在channel课程中公开Global。这样,当您的片段位于前景时,您就可以在片段中调用bindunbind

connectToPusher应该只创建一个频道并订阅它。

Global.java

private Channel channel;

public void connectTopusher() {
    PusherOptions options;
    Pusher pusher;
    options = new PusherOptions();
    options.setCluster("ap2");
    pusher = new Pusher("afbfc1f591fd7b70190f", options);
    pusher.connect();
    String profile = Global.shared().preferences.getString("PROFILE_ID", "");
    this.channel = pusher.subscribe(profile);
}

public Channel getChannel(){
    return this.channel;
}

然后在你的activity / fragment中你可以绑定/取消绑定你的侦听器,当它们被恢复/暂停时 - 只需像这样保持对它的引用:

YourActivity.java(也可能是你的片段)

private SubscriptionEventListener messageListener = new SubscriptionEventListener(){
     @Override
    public void onEvent(String channel, String event, String data) {
        //TODO: do something with events
    }
}

//Bind when the listener comes into the foreground:
@Override
protected void onResume() {
    super.onResume();

    ((Global) getActivity().getApplication()).getChannel().bind("message", messageListener);

}

//Make sure to unbind the event listener!
@Override
protected void onPause() {
    super.onPause();
    ((Global) getActivity().getApplication()).getChannel().unbind("message", messageListener);
}

我希望这会有所帮助:)