コード例 #1
0
        /// <summary>
        /// Register a device to the Azure notification hub
        /// </summary>
        /// <param name="userGuid">The user guid to associate the registration with</param>
        public void Register(string userGuid)
        {
            //Get token from APN registration process
            var token = NSUserDefaults.StandardUserDefaults["token"];


            var ConnectionString    = Constants.ConnectionString;
            var SharedKey           = Constants.SharedKey;
            var NotificationHubPath = Constants.NotificationHubPath;

            if (token != null)
            {
                var cs  = SBConnectionString.CreateListenAccess(new NSUrl(ConnectionString), SharedKey);
                var hub = new SBNotificationHub(cs, NotificationHubPath);

                hub.UnregisterAllAsync(token as NSData, (error) => {
                    if (error != null)
                    {
                        return;
                    }

                    var tags   = new NSSet(userGuid);
                    var expire = DateTime.Now.AddYears(1).ToString(CultureInfo.CreateSpecificCulture("en-US"));

                    NSError returnError;
                    hub.RegisterTemplate(token as NSData,
                                         "Template",
                                         "{\"aps\":{\"alert\":\"$(message)\", \"content-available\":\"#(silent)\", \"badge\":\"#(badge)\", \"sound\":\"$(sound)\"}, \"url\":\"$(url)\"}",
                                         expire,
                                         tags,
                                         out returnError);
                });
            }
        }
コード例 #2
0
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            var Hub = new SBNotificationHub(AppConstants.ListenConnectionString, AppConstants.NotificationHubName);

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

                var tags = new NSSet(AppConstants.SubscriptionTags.ToArray());
                Hub.RegisterNative(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.RegisterTemplate(deviceToken, "defaultTemplate", AppConstants.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                        }
                    }
                });
            });
        }
コード例 #3
0
ファイル: AppDelegate.cs プロジェクト: texamania/smart-store
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            // デバイスIDを記憶する
            byte[] result = new byte[deviceToken.Length];
            Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
            var deviceTokenEncoding = BitConverter.ToString(result).Replace("-", "");

            // デバイスIDを記憶する
            (App.Current as App).DeviceId = deviceTokenEncoding;

            Hub = new SBNotificationHub(Constant.ListenConnectionString, Constant.NotificationHubName);

            // update registration with Azure Notification Hub
            Hub.UnregisterAll(deviceToken, (error) =>
            {
                if (error != null)
                {
                    Analytics.TrackEvent("Unable to call unregister",
                                         new Dictionary <string, string> {
                        { nameof(error), error.ToString() }
                    });
                    return;
                }

                var tags = new NSSet(Constant.SubscriptionTags.ToArray());
                Hub.RegisterNative(deviceToken, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        Analytics.TrackEvent("RegisterNativeAsync",
                                             new Dictionary <string, string> {
                            { nameof(errorCallback), errorCallback.ToString() }
                        });
                    }
                });

                var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                Hub.RegisterTemplate(deviceToken, "defaultTemplate", Constant.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        if (errorCallback != null)
                        {
                            Analytics.TrackEvent("RegisterTemplateAsync",
                                                 new Dictionary <string, string> {
                                { nameof(errorCallback), errorCallback.ToString() }
                            });
                        }
                    }
                });
            });
        }
コード例 #4
0
        //Overrides the RegisteredForRemoteNotifications() function
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            Hub = new SBNotificationHub(AppConstants.ListenConnectionString, AppConstants.NotificationHubName);

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


                //Carlos's code to get guid
                var guid = Guid.NewGuid();
                //get guid and pass to database on signup
                //GlobalVars.user_guid = guid.ToString();
                var tag = "guid_" + guid.ToString();
                Debug.WriteLine("guid:" + tag);
                Preferences.Set("guid", tag);
                System.Diagnostics.Debug.WriteLine("This is the GUID from RegisteredForRemoteNotifications: " + Preferences.Get("guid", string.Empty));
                var tags = new NSSet(AppConstants.SubscriptionTags.Append(tag).ToArray());
                //End of Carlos's code


                //var tags = new NSSet(AppConstants.SubscriptionTags.ToArray());
                //Debug.WriteLine("tag = " + tags);
                //Debug.WriteLine("token = " + deviceToken);
                Hub.RegisterNative(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.RegisterTemplate(deviceToken, "defaultTemplate", AppConstants.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                        }
                    }
                });
            });
        }
コード例 #5
0
        public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            Hub = new SBNotificationHub(PushNotificationConstants.ListenConnectionString,
                                        PushNotificationConstants.NotificationHubName);

            await PushNotificationHelper.UpdateCurrentCountryTag();

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

                var tags = new NSSet(PushNotificationConstants.SubscriptionTags);
                Hub.RegisterNative(deviceToken, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                        UserDialogs.Instance.Toast("Error Registering to Push Notifications");
                    }
                });

                var templateExpiration = DateTime.Now.AddDays(120)
                                         .ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                Hub.RegisterTemplate(deviceToken, "defaultTemplate", PushNotificationConstants.APNTemplateBody,
                                     templateExpiration, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                            UserDialogs.Instance.Toast("Error Registering to Push Notifications template");
                        }
                    }
                });
            });
        }
コード例 #6
0
        private void SendPushTokenToAzure(NSData deviceToken)
        {
            try
            {
                var hub = new SBNotificationHub(Constants.PushNotificationsConstants.ListenConnectionString, Constants.PushNotificationsConstants.NotificationHubName);

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

                    var tags = new NSSet(Constants.PushNotificationsConstants.SubscriptionTags.ToArray());
                    hub.RegisterNative(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.RegisterTemplate(deviceToken, "defaultTemplate", Constants.PushNotificationsConstants.APNTemplateBody, templateExpiration, tags,
                                         (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                        }
                    });
                });
            }
            catch (Exception ex)
            {
                Debugger.Break();
                _logger.LogError(ex, "Failed to send push notification token to Azure Hub");
            }
        }