Xamarin.iOS .NET API / Azure Notification Hub notification notification tag

We are developing a cross-platform application (Android and iOS) using Xamarin.Forms. So far, we've managed to get the app to work just fine, so cool!

We have added several push notifications to our application using the Azure Notification Hub, GCM (for android) and APNS (for iOS). And it works almost perfectly!

In fact, we have the last problem: everything is fine for Android, and we can register for push notifications using iOS, but we cannot add some tags to our registrations.

Indeed, we should be able to send push notifications to one user or one group of users, and not to all. To do this, we do this in our web api method:

if (user.DeviceType.Equals("Android"))
{
   registration = new GcmRegistrationDescription(handles.Handle);

}
else
{
   registration = new AppleRegistrationDescription(handles.Handle);
}
registration.Tags = new HashSet<string>();
registration.Tags.Add("usermail:" + user.Email);
registration.Tags.Add("userid:" + user.Id);
registration.Tags.Add("userdevice:" + user.DeviceType);
registration.Tags.Add("usertype:" + tag);
registration.RegistrationId = handles.RegistrationId;
await NotificationHelper.Hub.CreateOrUpdateRegistrationAsync(registration);

Handle Android:

protected override void OnRegistered(Context context, string registrationId)
{
   [...] //the registration id is given in args
}

iOS:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
   [...]

   var DeviceToken = deviceToken.Description;

   if (!string.IsNullOrWhiteSpace(DeviceToken))
   {
      DeviceToken = DeviceToken.Trim('<').Trim('>');
   }

   UserInformations.Handles.RegistrationId = DeviceToken.Replace(" ", "").ToUpper();

   [...]
}

Android ( ), iOS.

NotificationHelper.Hub.CreateOrUpdateRegistrationAsync();

, , registrationId . , iOS, , , , , - .

, , , iOS APNS?

!

EDIT: , . , , . 2 , :

enter image description here enter image description here

, , , DeviceToken, , RegistrationId, ... , :/

+4
1

, , iOS "RegisteredForRemoteNotifications". , , .

"RegisteredForRemoteNotifications", , , , .

            if (oldDeviceToken != null)
            {
                if (oldDeviceToken.ToString() != deviceToken.ToString())
                {
                    try
                    {
                        Hub.UnregisterAllAsync(oldDeviceToken, (error) =>
                        {
                            //check for errors in unregistration process.
                            if (error != null)
                            {
                                TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Unregistering old device token against the notification hub.");
                                //exit out of the code here because we can't keep our hub clean without being able to remove the device from our registration list.
                                return;
                            }
                            else
                            {
                                ShouldComplete = true;
                            }
                        });
                    }
                    catch (Exception genEx)
                    {
                        TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine);
                    }
                }
            }
            else
            {
                // Store current device token 
                bool res = await ApplicationSettings.StoreDeviceToken(deviceToken);
            }

            // Check if we need to perform our initial registrations

            if (ShouldComplete)
            {
                NSSet RegisteredTags = await ApplicationSettings.RetrieveUserTags();

                if (RegisteredTags == null)
                {
                    RegisteredTags = new NSSet("AppleDevice");
                }

                //Register the device against the notification hub keeping the details accurate at all times.
                Hub.RegisterNativeAsync(deviceToken, RegisteredTags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + errorCallback + " | Source: " + "Registering device token against the notification hub.");
                    }
                    else
                    {
                        if (deviceToken != null)
                        {
                            NSUserDefaults.StandardUserDefaults.SetString("Completed", "InitialTagRegistration");
                            NSUserDefaults.StandardUserDefaults.Synchronize();
                        }
                    }
                });
            }
+1

Source: https://habr.com/ru/post/1673966/


All Articles