2016-09-13 4 views
0

私はSinchを使用してアプリを呼び出すためにappを実装するアプリケーションを作成しました。発信者が受信者の名前を知っている場合にのみ機能します。PubNubを使用してユーザーステータスを取得

これを克服するには、PubNubを使用してユーザー状態を取得することを提案してください。彼らはまたtutorial here.を持っています。問題は、チュートリアルが古く、PubNubがAPIを更新してからです。私は自分の新しいAPIを使用して自分のドキュメントを使って機能を実装しようとしましたが、それが動作していないか、またはより正確に私はそれを行う方法がわかりません。

私の現在のコードは次のとおりです。ここで

public class LoggedUsers extends Activity { 
    private PubNub pubNub; 
    String name; 
    private ArrayList users; 
    private JSONArray loggedUserList; 
    ListView UserList; 
    TextView allUsers; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.user_list); 
     allUsers = (TextView) findViewById(R.id.JSONFromPubNub); 
     SharedPreferences sp = getSharedPreferences("User_Details", MODE_APPEND); 
     try { 
      name = sp.getString("UserName", ""); 
     } catch (NullPointerException e) { 

     } 
     final PNConfiguration pnc = new PNConfiguration(); 
     pnc.setPublishKey("publish key"); 
     pnc.setSubscribeKey("subscribe key"); 
     pnc.setUuid(name); 

     pubNub = new PubNub(pnc); 
     users = new ArrayList<String>(); 
     UserList = (ListView) findViewById(R.id.listView); 
     String user = getUserStatus(); 
     allUsers.setText(user); 
     final ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.single_item_list, users); 
     UserList.setAdapter(adapter); 

     pubNub.addListener(new SubscribeCallback() { 
      @Override 
      public void status(PubNub pubnub, PNStatus status) { 
       if (status.getCategory() == PNStatusCategory.PNUnexpectedDisconnectCategory) { 
        // This event happens when radio/connectivity is lost 
        HashMap <String,String> map = new HashMap(); 
        map.put("State","Offline"); 
        pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid()); 
       } else if (status.getCategory() == PNStatusCategory.PNConnectedCategory) { 

        // Connect event. You can do stuff like publish, and know you'll get it. 
        // Or just use the connected event to confirm you are subscribed for 
        // UI/internal notifications, etc 
        HashMap <String,String> map = new HashMap(); 
        map.put("State","Online"); 
        pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid()); 
        /* if (status.getCategory() == PNStatusCategory.PNConnectedCategory) { 
         pubnub.publish().channel("awesomeChannel").message("hello!!").async(new PNCallback<PNPublishResult>() { 
          @Override 
          public void onResponse(PNPublishResult result, PNStatus status) { 
           // Check whether request successfully completed or not. 
           if (!status.isError()) { 

            // Message successfully published to specified channel. 
           } 
           // Request processing failed. 
           else { 

            // Handle message publish error. Check 'category' property to find out possible issue 
            // because of which request did fail. 
            // 
            // Request can be resent using: [status retry]; 
           } 
          } 
         }); 
        }*/ 
       } else if (status.getCategory() == PNStatusCategory.PNReconnectedCategory) { 
        HashMap <String,String> map = new HashMap(); 
        map.put("State","Online"); 
        pubNub.setPresenceState().channels(Arrays.asList("CallingChannel1")).state(map).uuid(pnc.getUuid()); 

        // Happens as part of our regular operation. This event happens when 
        // radio/connectivity is lost, then regained. 
       } else if (status.getCategory() == PNStatusCategory.PNDecryptionErrorCategory) { 

        // Handle messsage decryption error. Probably client configured to 
        // encrypt messages and on live data feed it received plain text. 
       } 
      } 

      @Override 
      public void message(PubNub pubnub, PNMessageResult message) { 

      } 

      @Override 
      public void presence(PubNub pubnub, PNPresenceEventResult presence) { 

      } 
     }); 
    } 
    public String getUserStatus(){ 
     final StringBuilder allUsers = new StringBuilder(); 
     pubNub.subscribe().channels(Arrays.asList("CallingChannel1")).withPresence().execute(); 
     pubNub.hereNow() 
       // tailor the next two lines to example 
       .channels(Arrays.asList("CallingChannel1")) 
       .includeState(true) 
       .includeUUIDs(true) 
       .async(new PNCallback<PNHereNowResult>() { 
        @Override 
        public void onResponse(PNHereNowResult result, PNStatus status) { 
         if (status.isError()) { 
          // handle error 
          return; 
         } 

         for (PNHereNowChannelData channelData : result.getChannels().values()) { 
          allUsers.append("---"); 
          allUsers.append("channel:" + channelData.getChannelName()); 
          allUsers.append("occoupancy: " + channelData.getOccupancy()); 
          allUsers.append("occupants:"); 
          for (PNHereNowOccupantData occupant : channelData.getOccupants()) { 
           allUsers.append("uuid: " + occupant.getUuid() + " state: " + occupant.getState()); 
          } 
         } 
        } 
       }); 
       return allUsers.toString(); 
    } 



    @Override 
    protected void onResume() { 
     super.onResume(); 
    } 
} 

は私の問題です:

  1. 私は(後でそれがリストビューに配置されます、私はTextViewの中で受信したすべてのデータを表示しようとしていますまたはリサイクラビュー)が表示されますが、空の画面が表示されているため、サーバーからnullが返されています。

  2. ユーザの状態は、ユーザが状態(オンライン→オフライン)を変更したかどうかを知るために常に更新されますが、コード内でasyncコールが行われていないように見えるので、一度だけ実行してからデータセット変更されていません。

問題を解決するにはどうすればよいですか?

+0

プレゼンスを使用して、オンラインと状態の変更を監視できます。あなたが購読すると、存在を有効にして購読すると、 'listener'の'存在 'コールバックに' state-change'、 'join'、' leave'& 'timeout'イベントが得られます。それが正しい方向に導くかどうかを教えてください。 –

+0

@CraigConover私はそれを調べます。基本的には、オンラインになっているユーザー(インターネットに接続され、フォアグラウンドまたはバックグラウンドでアプリケーションを実行しているユーザー)をリストビューに追加し、リストを非同期的に更新してユーザーの主な活動に影響を与えないようにします。 –

+0

@cjensen ..チュートリアルが古くなっていることを皆さんに知らせたいと思っています。あなたがそれを更新した場合に役立ちます。 –

答えて

1

PubNubプレゼンス

あなたはPubNub Presenceを使用してオンラインや状態の変化を監視することができます。登録すると、プレゼンスを有効にして購読すると、state-change,join,leave & timeoutevents in the presence callbackが得られます。

Callback callback = new Callback() { 
    @Override 
    public void successCallback(String channel, Object message) { 
     System.out.println(channel + " : " 
       + message.getClass() + " : " + message.toString()); 

     // take action on the presence events here 
    } 

    @Override 
    public void connectCallback(String channel, Object message) { 
     System.out.println("CONNECT on channel:" + channel 
       + " : " + message.getClass() + " : " 
       + message.toString()); 
    } 

    @Override 
    public void disconnectCallback(String channel, Object message) { 
     System.out.println("DISCONNECT on channel:" + channel 
       + " : " + message.getClass() + " : " 
       + message.toString()); 
    } 

    @Override 
    public void reconnectCallback(String channel, Object message) { 
     System.out.println("RECONNECT on channel:" + channel 
       + " : " + message.getClass() + " : " 
       + message.toString()); 
    } 

    @Override 
    public void errorCallback(String channel, PubnubError error) { 
     System.out.println("ERROR on channel " + channel 
       + " : " + error.toString()); 
    } 
}; 

try { 
    pubnub.presence("my_channel", callback); 
} 
catch (PubnubException e) { 
    System.out.println(e.toString()); 
} 

SinchはPubNub Android SDKのかなり古いバージョンを使用しているようです。 Sinchが同じバージョンのSDKを使用するための明示的な要件がない限り、Sinch SDKの外部で行う必要があることを行うには、まだPubNub Android SDK v4を使用できると思います。

+0

私はこれを試し、あなたに結果を知らせます。 –

関連する問題