2017-09-28 3 views
0

私は通知ハブを持っていて、それはハブ - 名前空間を持っていて、ほとんど満杯です。 私は別のハブを作成しますが、プログラマチックに(Android)を知るにはどのようにすればよいですか? Androidのソースコード:フルではないハブ通知を取得するにはどうすればよいですか?

public class NotificationSettings { 

    public static String SenderId = "MyFirebaseSenderId"; 
    public static String HubName = "myhub-hub"; 
    public static String HubListenConnectionString = "Endpoint=sb://myapp.serv.../;SharedAccessKeyName=D..ure;SharedAccessKey=K..t/n8I/X..="; 
} 

RegistrationIntent:今

public class RegistrationIntentService extends IntentService { 

    private static final String TAG = "RegIntentService"; 

    private NotificationHub hub; 

    public RegistrationIntentService() { 
     super(TAG); 
    } 

    public ApplicationUtils getApplicationUtils() { 
     return (ApplicationUtils) getApplication(); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     String resultString = null; 
     String regID = null; 
     String storedToken = null; 
     String tag = null; 
     try { 
      tag ="_UserId:" + getApplicationUtils().getUsuario().Id; 

     } catch (Exception e){ 
      return; 
     } 

     try { 
      if(FirebaseInstanceId.getInstance() == null){ 
      FirebaseApp.initializeApp(this); 
      } 

      String FCM_token = FirebaseInstanceId.getInstance().getToken(); 
      SaveSharedPreferences.setFCM(getApplicationContext(),FCM_token); 
      Log.d(TAG, "FCM Registration Token: " + FCM_token); 

      // Storing the registration id that indicates whether the generated token has been 
      // sent to your server. If it is not stored, send the token to your server, 
      // otherwise your server should have already received the token. 
      if (((regID=sharedPreferences.getString("registrationID", null)) == null)){ 

       NotificationHub hub = new NotificationHub(NotificationSettings.HubName, 
         NotificationSettings.HubListenConnectionString, this); 
       Log.d(TAG, "Attempting a new registration with NH using FCM token : " + FCM_token); 
       regID = hub.register(FCM_token, tag).getRegistrationId(); 

       // If you want to use tags... 
       // Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/ 
       // regID = hub.register(token, "tag1,tag2").getRegistrationId(); 

       resultString = "New NH Registration Successfully - RegId : " + regID; 
       Log.d(TAG, resultString); 

       sharedPreferences.edit().putString("registrationID", regID).apply(); 
       sharedPreferences.edit().putString("FCMtoken", FCM_token).apply(); 
      } 

      // Check if the token may have been compromised and needs refreshing. 
      else if (!((storedToken=sharedPreferences.getString("FCMtoken", "")).equals(FCM_token))) { 

       NotificationHub hub = new NotificationHub(NotificationSettings.HubName, 
         NotificationSettings.HubListenConnectionString, this); 
       Log.d(TAG, "NH Registration refreshing with token : " + FCM_token); 
       regID = hub.register(FCM_token, tag).getRegistrationId(); 

       // If you want to use tags... 
       // Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/ 
       // regID = hub.register(token, "tag1,tag2").getRegistrationId(); 

       resultString = "New NH Registration Successfully - RegId : " + regID; 
       Log.d(TAG, resultString); 

       sharedPreferences.edit().putString("registrationID", regID).apply(); 
       sharedPreferences.edit().putString("FCMtoken", FCM_token).apply(); 
      } 

      else { 
       resultString = "Previously Registered Successfully - RegId : " + regID; 
      } 
     } ............................................. 

が、この場合に重要であるかどうかはわからない、以下の私のdownendコードを次の。しかし、それはC#で開発されています.Net:

public static async void sendPushNotification(ApiController controller, DataObjects.Notification notification) 
     { 
      // Get the settings for the server project. 
      HttpConfiguration config = controller.Configuration; 

      MobileAppSettingsDictionary settings = 
       controller.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); 

      // Get the Notification Hubs credentials for the Mobile App. 
      string notificationHubName = settings.NotificationHubName; 
      string notificationHubConnection = settings 
       .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; 

      // Create a new Notification Hub client. 
      NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName); 

      // Android payload 
      JObject data = new JObject(); 

      data.Add("Id", notification.Id); 
      data.Add("Descricao", notification.Descricao); 
     ... 
      //alteração com a colocação da tag priority em caso de erro teste sem 
      var androidNotificationPayload = "{ \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}"; 

      try 
      { 
       // Send the push notification and log the results. 
       String tag = "_UserId:"+notification.Id_usuario; 
       //var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload); 
       var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload, tag); 

       // Write the success result to the logs. 
       config.Services.GetTraceWriter().Info(result.State.ToString()); 
      } 
      catch (System.Exception ex) 
      { 
       // Write the failure result to the logs. 
       config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error"); 
      } 
     } 

私はこの問題について多くの助けが必要です。 ありがとうございます。

答えて

1

あなたの説明によると、登録がいっぱいであるかどうかを確認した後で、通知ハブ(クライアント側ではない)に新しいユーザーを挿入するapiメソッドを書くことをお勧めします。

使用可能NotificationHubClient.GetAllRegistrationsAsyncすべてのAllRegistrationsをローカルに取得する方法。その後、その数を数えることができました。

正常にチェックされたら、入力ユーザーにどのハブをチェックします。

ワークフローは次のとおりです。

新規登録ユーザー:

クライアント:サーバーのWeb APIメソッドにユーザー情報を送る

サーバー:通知ハブがいっぱいになっているチェック(呼び出しNotificationHubClient.GetAllRegistrationsAsyncしますか、

さらに、通知ハブは異なる料金層を提供します。

複数のアクティブデバイスをサポートするために価格設定層を拡張することを検討することをお勧めします。

enter image description here

+0

Tks!私はこの方法を研究し、見つけた、私は200kユーザーを使用します。次はその上です。 –

関連する問題