トピックの購読
あるトピックに興味のあるユーザーは、このトピックを購読することでトピックに送信されたメッセージをプッシュ通知で受信できるようになります。
アプリケーションスコープのトピックを購読する場合、以下のようになります。この例では、SendingAlert
という既存のトピックを取得して、ログイン中のユーザーから購読しています。
-
try { // Instantiate an existing topic in the application scope. String topicName = "SendingAlert"; KiiTopic topic = Kii.topic(topicName); // Subscribe to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic); } catch (IOException ioe) { // Handle the the error. } catch (AppException e) { // Handle the the error. }
-
// Instantiate an existing topic in the application scope. String topicName = "SendingAlert"; KiiTopic topic = Kii.topic(topicName); // Subscribe to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic, new KiiPushCallBack() { @Override public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) { if (exception != null) { // Handle the error. return; } } });
グループスコープのトピックを購読する場合、以下のようになります。この例では、URL 指定されたグループより GroupTopic
という既存のトピックを取得して、ログイン中のユーザーから購読しています。
-
try { // Instantiate an existing group. KiiGroup group = KiiGroup.createByUri(groupUri); // Instantiate an existing topic in the group scope. String topicName = "GroupTopic"; KiiTopic topic = group.topic(topicName); // Subscribe to the topic. // (The current user must be a group member) KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic); } catch (IOException ioe) { // Handle the the error. } catch (AppException e) { // Handle the the error. }
-
// Instantiate an existing group. KiiGroup group = KiiGroup.createByUri(groupUri); // Instantiate an existing topic in the group scope. String topicName = "GroupTopic"; KiiTopic topic = group.topic(topicName); // Subscribe to the topic. // (The current user must be a group member) KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic, new KiiPushCallBack() { @Override public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) { if (exception != null) { // Handle the error. return; } } });
ユーザースコープのトピックを購読する場合、以下のようになります。この例では、ログイン中のユーザーの MyTODO
という既存のトピックを取得して、同じユーザーから購読しています。
-
try { // Instantiate an existing topic in the user scope. String topicName = "MyTODO"; KiiTopic topic = KiiUser.topic(topicName); // Subscribe to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic); } catch (IOException ioe ) { // Handle the error. } catch (AppException e) { // Handle the error. }
-
// Instantiate an existing topic in the user scope. String topicName = "MyTODO"; KiiTopic topic = KiiUser.topic(topicName); // Subscribe to the topic. KiiUser user = KiiUser.getCurrentUser(); KiiPushSubscription sub = user.pushSubscription(); sub.subscribe(topic, new KiiPushCallBack() { @Override public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) { if (exception != null) { // Handle the error. return; } } });
いずれの例においても、以下の処理が行われています。
- 購読対象となるトピックを特定。
- トピックの
pushSubscription
メソッドを実行し、KiiPushSubscription インスタンスを作成。 - 対象トピックを指定して
subscribe
メソッドを実行。