トピック購読の有無の確認
モバイルアプリのプログラムから、特定のトピックが購読済みかどうかを確認することができます。
以下に、ログイン中のユーザーがアプリケーションスコープのトピックを購読済みかどうかを確認する例を挙げます(グループスコープのトピックとユーザースコープのトピックについても、基本的な操作は同様です)。
-
// Instantiate the target topic. var topicName = "SendingAlert"; var topic = Kii.topicWithName(topicName); // Check if the current user is subscribed to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().isSubscribed(topic).then( function(params) { var thePushSubscription = params[0]; var theTopic = params[1]; var isSubscribed = params[2]; if(isSubscribed) { // The current user is subscribed to the topic. } else { // The current user is not subscribed to the topic. } } ).catch( function(error) { // Handle the error. var thePushSubscription = error.target; var errorString = error.message; } );
-
// Instantiate the target topic. var topicName = "SendingAlert"; var topic = Kii.topicWithName(topicName); // Check if the current user is subscribed to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().isSubscribed(topic, { success: function(thePushSubscription, theBucket, isSubscribed) { if(isSubscribed) { // The current user is subscribed to the topic. } else { // The current user is not subscribed to the topic. } }, failure: function(theTopic, errorString) { // Handle the error. } });
ここでは次の処理を行っています。
- 購読の有無を確認したいトピックのインスタンスを取得します。
pushSubscription
メソッドを実行してKiiPushSubscription
インスタンスを作成します。isSubscribed
メソッドを実行してトピック購読の有無を確認します。