トピック購読の有無の確認
モバイルアプリのプログラムから、特定のトピックが購読済みかどうかを確認することができます。
以下に、ログイン中のユーザーがアプリケーションスコープのトピックを購読済みかどうかを確認する例を挙げます(グループスコープのトピックとユーザースコープのトピックについても、基本的な操作は同様です)。
-
try { // Instantiate the target topic. String topicName = "SendingAlert"; KiiTopic topic = Kii.topic(topicName); // Check if the current user is subscribed to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); boolean isSubscribed = sub.isSubscribed(topic); if (isSubscribed) { // The current user is subscribed to the topic. } else { // The current user is not subscribed to the topic. } } catch (IOException ioe) { // Handle the error. } catch (AppException e) { // Handle the error. }
-
// Instantiate the target topic. String topicName = "SendingAlert"; KiiTopic topic = Kii.topic(topicName); // Check if the current user is subscribed to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.isSubscribed(topic, new KiiPushCallBack() { @Override public void onCheckSubscriptionCompleted(int taskId, KiiSubscribable target, boolean isSubscribed, Exception exception) { if (exception != null) { // Handle the error. return; } if (isSubscribed) { // The current user is subscribed to the topic. } else { // The current user is not subscribed to the topic. } } });
ここでは次の処理を行っています。
- 購読の有無を確認したいトピックのインスタンスを取得。
- ログイン中のユーザーの
pushSubscription
メソッドを実行し、KiiPushSubscription インスタンスを作成。 isSubscribed
メソッドを実行し、トピック購読の有無を確認。