Пример #1
0
        /// <summary>
        ///     A method to create and save a Category that will be registered for your app with all the actions
        ///     that were previously added for this Category Id and with hiddenPreviewsBodyPlaceholder and categorySummaryFormat.
        ///     See UNNotificationCategory.FromIdentifier.
        /// </summary>
        /// <remarks>
        ///     CategorySummaryFormat is only supported since iOS 12. For iOS 11 category will be created ignoring this option.
        /// </remarks>
        /// <param name="categoryId">The unique identifier for the Category. Should not be empty.</param>
        /// <param name="intentIdentifiers">
        ///     The intent identifier strings that you want to associate with notifications of this type.
        ///     The Intents framework defines constants for each type of intent that you can associate with your notifications.
        /// </param>
        /// <param name="hiddenPreviewsBodyPlaceholder">
        ///     A placeholder string to display when the user has disabled notification previews for the app.
        ///     Include the characters %u in the string to represent the number of notifications with the same thread identifier.
        /// </param>
        /// <param name="categorySummaryFormat">
        ///     Category’s summary format string.
        ///     Include the characters %u in the string to represent the number of notifications with the same thread identifier.
        /// </param>
        /// <param name="options">Additional options for handling notifications of this type.</param>
        protected void AddCategory(
            string categoryId,
            string[] intentIdentifiers,
            string hiddenPreviewsBodyPlaceholder,
            string categorySummaryFormat,
            UNNotificationCategoryOptions options)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
            {
                _actionsForCategories.TryGetValue(categoryId, out var actions);

                var messageCategory = UNNotificationCategory.FromIdentifier(
                    categoryId,
                    actions?.ToArray() ?? new UNNotificationAction[] { },
                    intentIdentifiers,
                    hiddenPreviewsBodyPlaceholder,
                    new NSString(categorySummaryFormat),
                    options);

                NotificationCategories.Add(messageCategory);
            }
            else
            {
                AddCategory(categoryId, intentIdentifiers, hiddenPreviewsBodyPlaceholder, options);
            }
        }
Пример #2
0
        static void RegisterUserNotificationCategories(NotificationUserCategory[] userCategories)
        {
            if (userCategories != null && userCategories.Length > 0)
            {
                usernNotificationCategories.Clear();
                IList <UNNotificationCategory> categories = new List <UNNotificationCategory>();
                foreach (var userCat in userCategories)
                {
                    IList <UNNotificationAction> actions = new List <UNNotificationAction>();

                    foreach (var action in userCat.Actions)
                    {
                        // Create action
                        var actionID = action.Id;
                        var title    = action.Title;
                        var notificationActionType = UNNotificationActionOptions.None;
                        switch (action.Type)
                        {
                        case NotificationActionType.AuthenticationRequired:
                            notificationActionType = UNNotificationActionOptions.AuthenticationRequired;
                            break;

                        case NotificationActionType.Destructive:
                            notificationActionType = UNNotificationActionOptions.Destructive;
                            break;

                        case NotificationActionType.Foreground:
                            notificationActionType = UNNotificationActionOptions.Foreground;
                            break;
                        }


                        var notificationAction =
                            UNNotificationAction.FromIdentifier(actionID, title, notificationActionType);

                        actions.Add(notificationAction);
                    }

                    // Create category
                    var categoryID          = userCat.Category;
                    var notificationActions = actions.ToArray() ?? new UNNotificationAction[] { };
                    var intentIDs           = new string[] { };
                    var categoryOptions     = new UNNotificationCategoryOptions[] { };

                    var category = UNNotificationCategory.FromIdentifier(categoryID, notificationActions, intentIDs,
                                                                         userCat.Type == NotificationCategoryType.Dismiss
                            ? UNNotificationCategoryOptions.CustomDismissAction
                            : UNNotificationCategoryOptions.None);

                    categories.Add(category);

                    usernNotificationCategories.Add(userCat);
                }

                // Register categories
                UNUserNotificationCenter.Current.SetNotificationCategories(
                    new NSSet <UNNotificationCategory>(categories.ToArray()));
            }
        }
Пример #3
0
        /// <summary>
        ///     A method to create and save a Category that will be registered for your app with all the actions
        ///     that were previously added for this Category Id. See UNNotificationCategory.FromIdentifier.
        /// </summary>
        /// <param name="categoryId">The unique identifier for the Category. Should not be empty.</param>
        /// <param name="intentIdentifiers">
        ///     The intent identifier strings that you want to associate with notifications of this type.
        ///     The Intents framework defines constants for each type of intent that you can associate with your notifications.
        /// </param>
        /// <param name="options">Additional options for handling notifications of this type.</param>
        protected void AddCategory(string categoryId, string[] intentIdentifiers, UNNotificationCategoryOptions options)
        {
            _actionsForCategories.TryGetValue(categoryId, out var actions);

            var messageCategory = UNNotificationCategory.FromIdentifier(
                categoryId,
                actions?.ToArray() ?? new UNNotificationAction[] { },
                intentIdentifiers,
                options);

            NotificationCategories.Add(messageCategory);
        }
Пример #4
0
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Dictionary <string, object> dict = (Dictionary <string, object>)PListCSLight.readPlist("Info.plist");

            Profile.EnableUpdatesOnAccessTokenChange(true);
            Facebook.CoreKit.Settings.AppID       = dict.GetValueOrDefault("FacebookAppID").ToString();
            Facebook.CoreKit.Settings.DisplayName = dict.GetValueOrDefault("FacebookDisplayName").ToString();

            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            ApplicationDelegate.SharedInstance.FinishedLaunching(app, options);


            var settings = UIUserNotificationSettings.GetSettingsForTypes(
                UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();


            //Create accept action
            var replyId     = "reply";
            var replyTitle  = "Reply";
            var replyAction = UNTextInputNotificationAction.FromIdentifier(replyId, replyTitle, UNNotificationActionOptions.None, "Reply", "Message");



            //Create category
            var categoryID      = "general";
            var actions         = new UNNotificationAction[] { replyAction };
            var intentIDs       = new string[] { };
            var categoryOptions = new UNNotificationCategoryOptions[] { };
            var category        = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

            //Register category
            var categories = new UNNotificationCategory[] { category };

            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories));



            UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();


            if (options != null && options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
            {
                appIsStarting = true;
            }

            return(base.FinishedLaunching(app, options));
        }
        public void whenCanUShare()
        {
            // Create action
            var actionID = "ten";
            var title    = "10 minutes";
            var action   = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);

            var actionID2 = "fifteen";
            var title2    = "15 minutes";
            var action2   = UNNotificationAction.FromIdentifier(actionID2, title2, UNNotificationActionOptions.None);

            var actionID3 = "thirty";
            var title3    = "30 minutes";
            var action3   = UNNotificationAction.FromIdentifier(actionID3, title3, UNNotificationActionOptions.None);

            var actionID4 = "leave";
            var title4    = "When I leave";
            var action4   = UNNotificationAction.FromIdentifier(actionID4, title4, UNNotificationActionOptions.None);

            // Create category
            var categoryID      = "remindlater";
            var actions         = new UNNotificationAction[] { action, action2, action3, action4 };
            var intentIDs       = new string[] { };
            var categoryOptions = new UNNotificationCategoryOptions[] { };
            var category        = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

            // Register category
            var categories = new UNNotificationCategory[] { category };

            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories));

            var content = new UNMutableNotificationContent();

            content.Title              = "Scuttle";
            content.Subtitle           = " ";
            content.Body               = "When can you share?";
            content.Badge              = 1;
            content.CategoryIdentifier = "remindlater";

            var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(2, false);

            var requestID = "sampleRequest";
            var request   = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

            UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
            {
                if (err != null)
                {
                    // Do something with error...
                }
            });
        }
Пример #6
0
        private void CreateNotificationCustomActions(UNMutableNotificationContent content)
        {
            var categoryIdentifier    = "foodpin.restaurantaction";
            var makeReservationAction = UNNotificationAction.FromIdentifier("foodpin.makeReservation", "Reserve a table", UNNotificationActionOptions.Foreground);
            var cancelAction          = UNNotificationAction.FromIdentifier("foodpin.cancel", "Later", UNNotificationActionOptions.None);

            UNNotificationAction[] actions = new UNNotificationAction[] { makeReservationAction, cancelAction };
            string[] intentIdentifiers     = new string[] { };
            UNNotificationCategoryOptions notificationCategoryOptions = new UNNotificationCategoryOptions();
            var category = UNNotificationCategory.FromIdentifier(categoryIdentifier, actions, intentIdentifiers, notificationCategoryOptions);

            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(category));
            content.CategoryIdentifier = categoryIdentifier;
        }
Пример #7
0
    private string ExtractOptionFromEnum(UNNotificationCategoryOptions option)
    {
        switch (option)
        {
        case UNNotificationCategoryOptions.UNNotificationCategoryOptionCustomDismissAction:
            return("custom_dismiss");

        case UNNotificationCategoryOptions.UNNotificationCategoryOptionAllowInCarPlay:
            return("carplay");

        default:
            return("");
        }
    }
Пример #8
0
        //
        // This method is invoked when the application has loaded and is ready to run. In this
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            var settings = UIUserNotificationSettings.GetSettingsForTypes(
                UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();


            //Create accept action
            var replyId     = "reply";
            var replyTitle  = "Reply";
            var replyAction = UNTextInputNotificationAction.FromIdentifier(replyId, replyTitle, UNNotificationActionOptions.None, "Reply", "Message");



            //Create category
            var categoryID      = "general";
            var actions         = new UNNotificationAction[] { replyAction };
            var intentIDs       = new string[] { };
            var categoryOptions = new UNNotificationCategoryOptions[] { };
            var category        = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

            //Register category
            var categories = new UNNotificationCategory[] { category };

            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories));



            UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();


            if (options != null && options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
            {
                appIsStarting = true;
            }

            return(base.FinishedLaunching(app, options));
        }
Пример #9
0
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            // Code for starting up the Xamarin Test Cloud Agent
#if ENABLE_TEST_CLOUD
            Xamarin.Calabash.Start();
#endif

            LoadApplication(new App());


            // Handling Push notification when app is closed if App was opened by Push Notification...
            if (options != null && options.Keys != null && options.Keys.Count() != 0 && options.ContainsKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")))
            {
                NSDictionary UIApplicationLaunchOptionsRemoteNotificationKey = options.ObjectForKey(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey")) as NSDictionary;

                ProcessNotification(UIApplicationLaunchOptionsRemoteNotificationKey, true);
            }

            //added on 1/7/17 by aditmer to see if the device token from APNS has changed (like after an app update)
            if (ApplicationSettings.InstallationID != "")
            {
                //Push notifications registration
                if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                        new NSSet());

                    UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
                else
                {
                    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
                }
            }
            //added on 1/4/17 by aditmer to add 4 (1/6/17) customizable snooze options
            int snoozeTime1 = 1;
            int snoozeTime2 = 2;
            int snoozeTime3 = 3;
            int snoozeTime4 = 4;

            if (ApplicationSettings.AlarmUrgentLowMins1 != 0)
            {
                snoozeTime1 = ApplicationSettings.AlarmUrgentLowMins1;
            }

            if (ApplicationSettings.AlarmUrgentLowMins2 != 0)
            {
                snoozeTime2 = ApplicationSettings.AlarmUrgentLowMins2;
            }

            if (ApplicationSettings.AlarmUrgentMins1 != 0)
            {
                snoozeTime3 = ApplicationSettings.AlarmUrgentMins1;
            }

            if (ApplicationSettings.AlarmUrgentMins2 != 0)
            {
                snoozeTime4 = ApplicationSettings.AlarmUrgentMins2;
            }


            //added on 12/03/16 by aditmer to add custom actions to the notifications (I think this code goes here)
            // Create action
            var actionID = "snooze1";
            var title    = $"Snooze {snoozeTime1} minutes";
            var action   = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.None);

            var actionID2 = "snooze2";
            var title2    = $"Snooze {snoozeTime2} minutes";
            var action2   = UNNotificationAction.FromIdentifier(actionID2, title2, UNNotificationActionOptions.None);

            var actionID3 = "snooze3";
            var title3    = $"Snooze {snoozeTime3} minutes";
            var action3   = UNNotificationAction.FromIdentifier(actionID3, title3, UNNotificationActionOptions.None);

            var actionID4 = "snooze4";
            var title4    = $"Snooze {snoozeTime4} minutes";
            var action4   = UNNotificationAction.FromIdentifier(actionID4, title4, UNNotificationActionOptions.None);

            // Create category
            var categoryID = "event";
            var actions    = new List <UNNotificationAction> {
                action, action2, action3, action4
            };
            var intentIDs       = new string[] { };
            var categoryOptions = new UNNotificationCategoryOptions[] { };

            //added on 1/19/17 by aditmer to remove duplicate snooze options (they can be custom set by each user in their Nightscout settings)
            actions = actions.DistinctBy((arg) => arg.Title).ToList();


            var category = UNNotificationCategory.FromIdentifier(categoryID, actions.ToArray(), intentIDs, UNNotificationCategoryOptions.AllowInCarPlay);

            // Register category
            var categories = new UNNotificationCategory[] { category };
            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories));

            return(base.FinishedLaunching(app, options));
        }
        //public UserNotificationDelegate()
        //{
        //var content = new UNMutableNotificationContent();
        //content.Title = "Scuttle";
        //content.Subtitle = " ";
        //content.Body = "Hey! Are you at "+App.RestoName;
        //content.Badge = 1;

        //var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false);

        //var requestID = "sampleRequest";
        //var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

        //UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
        //{
        //    if (err != null)
        //    {
        //        // Do something with error...
        //    }
        //});

        //WillPresentNotification(UNUserNotificationCenter.Current, content, (UNNotificationPresentationOptions obj) =>
        //{

        //});
        //}
        #region ask if the user is in a restaurant
        public void sendLocalNotif(string name)
        {
            if (name.Equals("reminder"))
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    //var actionID = "yes2";
                    //var title = "Yes";
                    //var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.None);

                    //var actionID2 = "no2";
                    //var title2 = "No";
                    //var action2 = UNNotificationAction.FromIdentifier(actionID2, title2, UNNotificationActionOptions.None);

                    //// Create category
                    //var categoryID = "message";
                    //var actions = new UNNotificationAction[] { action, action2 };
                    //var intentIDs = new string[] { };
                    //var categoryOptions = new UNNotificationCategoryOptions[] { };
                    //var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

                    //// Register category
                    //var categories = new UNNotificationCategory[] { category };
                    //UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));


                    var content = new UNMutableNotificationContent();
                    content.Title    = "Scuttle";
                    content.Subtitle = " ";
                    content.Body     = "Are you ready to share your experience?";
                    content.Badge    = 1;
                    //content.CategoryIdentifier = "message";

                    var trigger   = UNTimeIntervalNotificationTrigger.CreateTrigger(2, false);
                    var requestID = "sampleRequest";
                    var request   = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

                    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
                    {
                        if (err != null)
                        {
                            // Do something with error...
                        }
                    });
                }
                else
                {
                    // create the notification
                    var notification = new UILocalNotification();
                    // set the fire date (the date time in which it will fire)
                    notification.FireDate = NSDate.FromTimeIntervalSinceNow(2);
                    // configure the alert
                    notification.AlertTitle  = "Reminder2";
                    notification.AlertAction = "Scuttle";
                    notification.AlertBody   = "Are you ready to share your experience?";

                    // modify the badge
                    notification.ApplicationIconBadgeNumber = 1;

                    // set the sound to be the default sound
                    notification.SoundName = UILocalNotification.DefaultSoundName;

                    // schedule it
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                }
            }
            else
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    // Create action
                    var actionID = "yes";
                    var title    = "Yes. Share my experience!";
                    var action   = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);

                    var actionID3 = "RemindMeLater";
                    var title3    = "Yes. Remind me later to share";
                    var action3   = UNNotificationAction.FromIdentifier(actionID3, title3, UNNotificationActionOptions.None);
                    var actionID2 = "no";
                    var title2    = "Not this time";
                    var action2   = UNNotificationAction.FromIdentifier(actionID2, title2, UNNotificationActionOptions.None);

                    // Create category
                    var categoryID      = "message";
                    var actions         = new UNNotificationAction[] { action, action3, action2 };
                    var intentIDs       = new string[] { };
                    var categoryOptions = new UNNotificationCategoryOptions[] { };
                    var category        = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

                    // Register category
                    var categories = new UNNotificationCategory[] { category };
                    UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories));

                    var content = new UNMutableNotificationContent();
                    content.Title              = "Scuttle";
                    content.Subtitle           = " ";
                    content.Body               = "Hey! Are you at " + name + "?";
                    content.Badge              = 1;
                    content.CategoryIdentifier = "message";

                    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(2, false);

                    var requestID = "sampleRequest";
                    var request   = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

                    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
                    {
                        if (err != null)
                        {
                            // Do something with error...
                        }
                    });
                }
                else
                {
                    // create the notification
                    var notification = new UILocalNotification();

                    // set the fire date (the date time in which it will fire)
                    notification.FireDate = NSDate.FromTimeIntervalSinceNow(5);

                    // configure the alert
                    notification.AlertTitle  = "LocationCheck";
                    notification.AlertAction = "Scuttle";
                    notification.AlertBody   = "Hey! Are you at " + name + "?";

                    // modify the badge
                    notification.ApplicationIconBadgeNumber = 1;

                    // set the sound to be the default sound
                    notification.SoundName = UILocalNotification.DefaultSoundName;

                    // schedule it
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                }
            }
        }