连接到 Android 应用中的 Pusher,一次还是多次?

huangapple go评论112阅读模式
英文:

Connect to pusher in android app, multiple or once?

问题

以下是您要翻译的内容:

在我的应用程序(流媒体系列、电影)中,我有一个部分供用户可以为系列或电影设置提醒。我使用Pusher来接收有关提醒数据的服务器消息。

是否正确:我需要为提醒列表中的每个项目连接到一个频道?还是我应该连接到Pusher一次,然后在Pusher事件中获取相关的系列/电影消息?(服务器端为每个提醒列表项实现了Pusher,我们是否应该更改服务器端实现,还是我可以为每个项目连接到Pusher?)

这是我的Pusher实现:

public Pusher getPusher() throws Exception {
    if (pusher == null) {
        HttpAuthorizer auth = new HttpAuthorizer(BuildConfig.PUSHER);
        HashMap<String, String> authHeader = new HashMap<>();
        authHeader.put("Authorization", SharedPref.INSTANCE.read(AUTH_TOKEN, ""));
        auth.setHeaders(authHeader);
        PusherOptions option = new PusherOptions();
        option.setCluster(BuildConfig.PUSHER_CLUSTER);
        option.setAuthorizer(auth);
        pusher = new Pusher(BuildConfig.PUSHER_KEY, option);

        pusher.subscribePrivate("private-app_ch." + serialId, new PrivateChannelEventListener() {
            @Override
            public void onAuthenticationFailure(String s, Exception e) {
                Timber.i("pusher onAuthenticationFailure " + e.getMessage());
            }

            @Override
            public void onSubscriptionSucceeded(String s) {
                Timber.i("pusher onSubscriptionSucceeded: " + s);
            }

            @Override
            public void onEvent(String s, String s1, String result) {
                Timber.i("pusher onEvent" + s + ":" + s1);
                Timber.i("pusher onEvent" + result);
            }
        }, "App\\Events\\AppBroadcastEvent");
    }
    return pusher;
}
英文:

In my app (streaming series, movies) I have a section for users that can set Reminder for the series or movies. And I implement Pusher to receive server message for reminding data.

Is it true that I connect to channel for each item in the reminder list?? or I should connect to the pusher once and in the pusher event get related series/ movies message?(Server-side implemented pusher for each reminder list items, should we change server-side implementation or I can connect to pusher for each items? )

This is my Implementation for pusher:

 public Pusher getPusher() throws Exception {
        if (pusher == null) {
            HttpAuthorizer auth = new HttpAuthorizer(BuildConfig.PUSHER);
            HashMap&lt;String, String&gt; authHeader = new HashMap&lt;&gt;();
            authHeader.put(&quot;Authorization&quot;, SharedPref.INSTANCE.read(AUTH_TOKEN, &quot;&quot;));
            auth.setHeaders(authHeader);
            PusherOptions option = new PusherOptions();
            option.setCluster(BuildConfig.PUSHER_CLUSTER);
            option.setAuthorizer(auth);
            pusher = new Pusher(BuildConfig.PUSHER_KEY, option);

        
            pusher.subscribePrivate(&quot;private-app_ch.&quot; + serialId, new PrivateChannelEventListener() {
                @Override
                public void onAuthenticationFailure(String s, Exception e) {
                    Timber.i(&quot;pusher onAuthenticationFailure &quot; + e.getMessage());
                }

                @Override
                public void onSubscriptionSucceeded(String s) {
                    Timber.i(&quot;pusher onSubscriptionSucceeded: &quot; + s);
                }

                @Override
                public void onEvent(String s, String s1, String result) {
                    Timber.i(&quot;pusher onEvent&quot; + s + &quot;:&quot; + s1);
                    Timber.i(&quot;pusher onEvent&quot; + result);

                    
                }
            }, &quot;App\\Events\\AppBroadcastEvent&quot;);
        }
        return pusher;
    }

答案1

得分: 1

最佳做法是在Channels上维护一个连接,但为提醒列表中的每个项目创建一个订阅

因此,您将为提醒列表中的每个项目调用pusher.subscribePrivate,然后在服务器端需要发送提醒时,向每个单独的通道发布。

例如,如果用户想要收到有关《陌生人事务所》和《橙是新黑》的提醒,您将订阅这两个项目:

pusher.subscribePrivate("private-app_ch.strangerthings"

pusher.subscribePrivate("private-app_ch.orangeisthenewblack"

然后,您的服务器将向《陌生人事务所》通道发布有关《陌生人事务所》的提醒,向OISTNB通道发布有关OISTNB的提醒,依此类推。

通过这种方式,只有相关的更新会发送到客户端(服务器端过滤)。如果只订阅一个通道,客户端将收到他们可能不想要更新的消息,您将不得不在客户端进行过滤。

这也在这里有解释:https://support.pusher.com/hc/en-us/articles/360025398514-Should-i-subscribe-to-lots-of-Channels-

还值得考虑的另一点是,Channels仅在应用程序打开时保持活动连接。当应用程序进入后台/关闭时,连接将被关闭。这意味着要发送提醒,用户必须始终在您的应用程序中。您可能还希望在应用程序关闭时发送推送通知,以便用户不会错过提醒。

英文:

The best practice for this would be to maintain one connection to Channels but make a subscription for each item in the reminder list.

So you would call pusher.subscribePrivate for each item in the reminder list and then on the server side publish to each individual Channel when a reminder needs to be sent.

For example if a user wanted to be reminded about 'Stranger Things' and 'Orange is the new black' you would subscribe to both:

pusher.subscribePrivate(&quot;private-app_ch.strangerthings&quot;

and

pusher.subscribePrivate(&quot;private-app_ch.orangeisthenewblack&quot;

Your server would then publish reminders about 'Stranger Things' to the Stranger things channel and OISTNB to the OISTNB channel and so on.

This way only relevant updates are sent to the client (server-side filtering). If you only subscribe to one channel the client will get messages they may not want updates about and you would have to filter these out on the client side.

This is also explained here: https://support.pusher.com/hc/en-us/articles/360025398514-Should-i-subscribe-to-lots-of-Channels-

One additional point that is worth considering is that Channels will only maintain an active connection when the app is open. The connection will be closed when the app is backgrounded/closed. This means for reminders to be sent the user would always have to be in your app. You may want to consider also sending push notifications when the app is closed so the user does not miss reminders.

huangapple
  • 本文由 发表于 2020年3月15日 20:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定