如何获取未满的集线器通知?

How to get the hub notification that is not full?

我有一个通知中心,它有一个中心名称空间,它几乎已满。 我将创建另一个集线器,我如何以编程方式知道 (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..=";
}

注册意向:

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;
            }
        } .............................................

现在,它遵循我下面的下行代码,我不知道在这种情况下它是否重要。但是,它是用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");
            }
        }

在这个问题上我需要很多帮助。 非常感谢。

根据您的描述,我建议您编写一个 api 方法,在检查注册是否已满后将新用户插入通知中心(而不是在客户端)。

我们可以使用 NotificationHubClient.GetAllRegistrationsAsync 方法将所有 AllRegistrations 获取到本地。然后我们可以数一数它的数量。

检查成功后,我们再检查输入用户到哪个集线器。

工作流程是:

新用户注册:

客户端:将用户信息发送到服务器webapi方法

服务器:检查通知中心是否已满(通过调用NotificationHubClient.GetAllRegistrationsAsync 或如果注册到新中心失败则直接注册用户)

此外,通知中心提供不同的定价层。

我建议您可以考虑扩大定价等级以支持多个活动设备。