Example #1
0
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            var tagArray = ProducerClient.Shared.UserRole.GetTagArray();

            var notificationHub = new SBNotificationHub(Settings.NotificationsConnectionString, Settings.NotificationsName);

            Log.Debug($"Registering with Azure Notification Hub '{Settings.NotificationsName}' with Tags ({string.Join (ConstantStrings.Comma, tagArray)})");

            var tags = new NSSet(tagArray);

            var expire = DateTime.Now.AddDays(90).ToString(CultureInfo.CreateSpecificCulture("en-US"));

            notificationHub.RegisterTemplateAsync(deviceToken, nameof(PushTemplate), PushTemplate.iOS, expire, tags, err =>
            {
                if (err != null)
                {
                    Log.Error($"{err.Description}");
                }
                else
                {
                    var token = deviceToken.ToString().Replace(" ", string.Empty).Trim('<', '>');

                    Log.Debug($"Successfully Registered for Notifications. (device token: {token})");
                }
            });
        }
Example #2
0
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            Hub = new SBNotificationHub(AppConstants.ListenConnectionString, AppConstants.NotificationHubName);

            // update registration with Azure Notification Hub
            Hub.UnregisterAllAsync(deviceToken, (error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister {error}");
                    return;
                }

                var tags = new NSSet(AppConstants.SubscriptionTags.ToArray());
                Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                    }
                });

                var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                Hub.RegisterTemplateAsync(deviceToken, "defaultTemplate", AppConstants.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                        }
                    }
                });
            });
        }
Example #3
0
        public void SetToken(object deviceToken)
        {
            if (hub == null)
            {
                hub = new SBNotificationHub("Endpoint=sb://xamsnap.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=ZqbSdT8i+2YBpNNxmgMNAOrnoGPCBcr+/Hs9gecyNTQ=", "xamsnap");
            }

            string template = "{\"aps\": {\"alert\": \"$(message)\"}}";
            var    tags     = new NSSet(userName);

            hub.RegisterTemplateAsync((NSData)deviceToken, "iOS", template, DateTime.Now.AddDays(90).ToString(enUS), tags, errorCallback =>
            {
                if (errorCallback != null)
                {
                    Console.WriteLine("RegisterNativeAsync error: " + errorCallback);
                }
            });
        }
Example #4
0
        public static void RegisterNotificationHub(string[] tags)
        {
            if (CoreSettings.DeviceToken != null)
            {
                var listenConnection = CoreSettings.Config.AzurePushSettings.ListenConnectionString;
                var notificationName = CoreSettings.Config.AzurePushSettings.NotificationHubName;
                var apnsTemplate     = CoreSettings.Config.AzurePushSettings.APNTemplateBody;

                Hub = new SBNotificationHub(listenConnection, notificationName);

                // update registration with Azure Notification Hub
                Hub.UnregisterAllAsync(CoreSettings.DeviceToken, (error) =>
                {
                    if (error != null)
                    {
                        Debug.WriteLine($"Unable to call unregister {error}");
                    }

                    var nsTags = new NSSet(tags);
                    Hub.RegisterNativeAsync(CoreSettings.DeviceToken, nsTags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                        }
                    });

                    var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                    Hub.RegisterTemplateAsync(CoreSettings.DeviceToken, "defaultTemplate", apnsTemplate, templateExpiration, nsTags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            if (errorCallback != null)
                            {
                                Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                            }
                        }
                    });
                });
            }
        }
Example #5
0
        public static async Task RegisterNotificationHub(string[] tags)
        {
            if (CoreSettings.DeviceToken != null)
            {
                var listenConnection = CoreSettings.Config.AzurePushSettings.ListenConnectionString;
                var notificationName = CoreSettings.Config.AzurePushSettings.NotificationHubName;
                var apnsTemplate     = CoreSettings.Config.AzurePushSettings.APNTemplateBody;

                Hub = new SBNotificationHub(listenConnection, notificationName);

                try
                {
                    await Hub.UnregisterAllAsync(CoreSettings.DeviceToken);

                    var nsTags = new NSSet(tags);
                    try
                    {
                        await Hub.RegisterNativeAsync(CoreSettings.DeviceToken, nsTags);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"RegisterNativeAsync error: {ex.Message}");
                    }


                    var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                    try
                    {
                        await Hub.RegisterTemplateAsync(CoreSettings.DeviceToken, "defaultTemplate", apnsTemplate, templateExpiration, nsTags);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"RegisterTemplateAsync error: {ex.Message}");
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Unable to call unregister {ex.Message}");
                }
            }
        }
Example #6
0
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            var connectionString = SBConnectionString.CreateUsingSharedAccessSecretWithListenAccess(
                new NSUrl(@"sb://" + hubNamespace + ".servicebus.windows.net"), key);

            this._hub = new SBNotificationHub(connectionString, hubName);

            _hub.UnregisterAllAsync(deviceToken, (error) => {
                if (error != null)
                {
                    Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                }
                else
                {
                    NSSet tags = null;                     // create tags if you want
                    _hub.RegisterTemplateAsync(deviceToken, "App2000iOSRegistration", @"{""aps"": {""alert"": ""$(msg)""}}", @"$(expiryProperty)", tags, (registrationError) => {
                        if (registrationError != null)
                        {
                            Console.WriteLine("Error calling RegisterTemplate: {0}", registrationError.ToString());
                        }
                    });
                }
            });
        }