Пример #1
0
        public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            var    parameters = GetParameters(response.Notification.Request.Content.UserInfo);
            string?result     = null;
            NotificationCategoryType catType = NotificationCategoryType.Default;

            if (response.IsCustomAction)
            {
                catType = NotificationCategoryType.Custom;
            }
            else if (response.IsDismissAction)
            {
                catType = NotificationCategoryType.Dismiss;
            }


            if (response is UNTextInputNotificationResponse textResponse)
            {
                result = textResponse.UserText;
            }

            var notificationResponse = new NotificationResponse(parameters, $"{response.ActionIdentifier}".Equals("com.apple.UNNotificationDefaultActionIdentifier", StringComparison.CurrentCultureIgnoreCase) ? string.Empty : $"{response.ActionIdentifier}", catType, result);

            _onNotificationAction?.Invoke(this, new PushNotificationResponseEventArgs(notificationResponse.Data, notificationResponse.Identifier, notificationResponse.Type, result));

            CrossPushNotification.Current.NotificationHandler?.OnAction(notificationResponse);

            // Inform caller it has been handled
            completionHandler();
        }
Пример #2
0
        public static void Initialize(NSNotification notification, bool autoRegistration = true, bool enableDelayedResponse = true)
        {
            CrossPushNotification.Current.NotificationHandler = CrossPushNotification.Current.NotificationHandler ?? new DefaultPushNotificationHandler();

            if (notification != null && notification.UserInfo != null)
            {
                var parameters = GetParameters(notification.UserInfo);

                var notificationResponse = new NotificationResponse(parameters, string.Empty, NotificationCategoryType.Default);


                if (_onNotificationOpened == null && enableDelayedResponse)
                {
                    delayedNotificationResponse = notificationResponse;
                }
                else
                {
                    _onNotificationOpened?.Invoke(CrossPushNotification.Current, new PushNotificationResponseEventArgs(notificationResponse.Data, notificationResponse.Identifier, notificationResponse.Type));
                }

                CrossPushNotification.Current.NotificationHandler?.OnOpened(notificationResponse);
            }

            if (autoRegistration)
            {
                CrossPushNotification.Current.RegisterForPushNotifications();
            }
        }
Пример #3
0
        public static void Initialize(NSDictionary options, bool autoRegistration = true, bool enableDelayedResponse = true)
        {
            CrossPushNotification.Current.NotificationHandler = CrossPushNotification.Current.NotificationHandler ?? new DefaultPushNotificationHandler();

            if (options?.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey) ?? false)
            {
                var pushPayload = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
                if (pushPayload != null)
                {
                    var parameters = GetParameters(pushPayload);

                    var notificationResponse = new NotificationResponse(parameters, string.Empty, NotificationCategoryType.Default);

                    if (_onNotificationOpened == null && enableDelayedResponse)
                    {
                        delayedNotificationResponse = notificationResponse;
                    }
                    else
                    {
                        _onNotificationOpened?.Invoke(CrossPushNotification.Current, new PushNotificationResponseEventArgs(notificationResponse.Data, notificationResponse.Identifier, notificationResponse.Type));
                    }

                    CrossPushNotification.Current.NotificationHandler?.OnOpened(notificationResponse);
                }
            }

            if (autoRegistration)
            {
                CrossPushNotification.Current.RegisterForPushNotifications();
            }
        }
        public static void ProcessIntent(Activity activity, Intent intent, bool enableDelayedResponse = true)
        {
            DefaultNotificationActivityType = activity.GetType();

            Bundle extras = intent?.Extras;

            if (extras != null && !extras.IsEmpty)
            {
                var parameters = new Dictionary <string, object>();
                foreach (var key in extras.KeySet())
                {
                    if (!parameters.ContainsKey(key) && extras.Get(key) != null)
                    {
                        parameters.Add(key, $"{extras.Get(key)}");
                    }
                }

                NotificationManager manager =
                    _context.GetSystemService(Context.NotificationService) as NotificationManager;
                var notificationId = extras.GetInt(DefaultPushNotificationHandler.ActionNotificationIdKey, -1);
                if (notificationId != -1)
                {
                    var notificationTag = extras.GetString(DefaultPushNotificationHandler.ActionNotificationTagKey,
                                                           string.Empty);
                    if (notificationTag == null)
                    {
                        manager.Cancel(notificationId);
                    }
                    else
                    {
                        manager.Cancel(notificationTag, notificationId);
                    }
                }


                var response = new NotificationResponse(parameters,
                                                        extras.GetString(DefaultPushNotificationHandler.ActionIdentifierKey, string.Empty));

                if (_onNotificationOpened == null && enableDelayedResponse)
                {
                    delayedNotificationResponse = response;
                }
                else
                {
                    _onNotificationOpened?.Invoke(CrossPushNotification.Current,
                                                  new PushNotificationResponseEventArgs(response.Data, response.Identifier, response.Type));
                }

                CrossPushNotification.Current.NotificationHandler?.OnOpened(response);
            }
        }
        public void OnBackgroundActivated(BackgroundActivatedEventArgs args)
        {
            var deferral = args.TaskInstance.GetDeferral();

            switch (args.TaskInstance.Task.Name)
            {
            case "ToastBackgroundTask":
                var details = args.TaskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
                if (details != null)
                {
                    string arguments = details.Argument;
                    var    userInput = details.UserInput;
                    NotificationResponse notificationResponse;
                    PushNotificationResponseEventArgs notificationArgs;
                    // Perform tasks
                    if (userInput.Any())
                    {
                        var input = userInput.FirstOrDefault();
                        var dict  = new Dictionary <string, object>()
                        {
                            { NotificationArgumentKey, details.Argument },
                            { NotificationInputsKey, details.UserInput.ToDictionary(d => d.Key, d => d.Value) }
                        };
                        notificationArgs     = new PushNotificationResponseEventArgs(dict, input.Key, result: $"{input.Value}");
                        notificationResponse = new NotificationResponse(dict, input.Key, result: $"{input.Value}");
                    }
                    else
                    {
                        var dict = new Dictionary <string, object>()
                        {
                            { NotificationArgumentKey, details.Argument }
                        };
                        notificationArgs     = new PushNotificationResponseEventArgs(dict);
                        notificationResponse = new NotificationResponse(dict);
                    }


                    OnNotificationAction?.Invoke(this, notificationArgs);

                    CrossPushNotification.Current.NotificationHandler?.OnAction(notificationResponse);
                }
                break;
            }

            deferral.Complete();
        }
        public async Task OnLaunchedOrActivated(IActivatedEventArgs e)
        {
            await RegisterBackgroundTask();

            // Handle toast activation
            if (e is ToastNotificationActivatedEventArgs)
            {
                var    details   = e as ToastNotificationActivatedEventArgs;
                string arguments = details.Argument;
                var    userInput = details.UserInput;

                NotificationResponse notificationResponse;
                PushNotificationResponseEventArgs notificationArgs;
                // Perform tasks
                if (userInput.Any())
                {
                    var input = userInput.FirstOrDefault();
                    var dict  = new Dictionary <string, object>()
                    {
                        { NotificationArgumentKey, details.Argument },
                        { NotificationInputsKey, details.UserInput.ToDictionary(d => d.Key, d => d.Value) }
                    };
                    notificationArgs     = new PushNotificationResponseEventArgs(dict, input.Key, result: $"{input.Value}");
                    notificationResponse = new NotificationResponse(dict, input.Key, result: $"{input.Value}");
                }
                else
                {
                    var dict = new Dictionary <string, object>()
                    {
                        { NotificationArgumentKey, details.Argument }
                    };
                    notificationArgs     = new PushNotificationResponseEventArgs(dict);
                    notificationResponse = new NotificationResponse(dict);
                }


                OnNotificationOpened?.Invoke(this, notificationArgs);

                CrossPushNotification.Current.NotificationHandler?.OnOpened(notificationResponse);
            }
        }
Пример #7
0
 public virtual void OnOpened(NotificationResponse response)
 {
     System.Diagnostics.Debug.WriteLine($"{DomainTag} - OnOpened");
 }
 public void OnAction(NotificationResponse response)
 {
     System.Diagnostics.Debug.WriteLine($"{DomainTag} - OnAction");
 }
        internal static void RegisterAction(IDictionary <string, object> data, string result = null)
        {
            var response = new NotificationResponse(data, data.ContainsKey(DefaultPushNotificationHandler.ActionIdentifierKey) ? $"{data[DefaultPushNotificationHandler.ActionIdentifierKey]}" : string.Empty, NotificationCategoryType.Default, result);

            _onNotificationAction?.Invoke(CrossPushNotification.Current, new PushNotificationResponseEventArgs(response.Data, response.Identifier, response.Type));
        }