Exemple #1
0
        public override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action <UIBackgroundFetchResult> completionHandler)
        {
            var notificationType = userInfo["nt"]?.ToString();

            Logger.Log("AppDelegate.DidReceiveRemoteNotification: NT: {0}", notificationType);

            try
            {
                switch (notificationType)
                {
                case RemoteNotificationType.Notification:
                    var nid = userInfo["nid"]?.ToString();
                    Logger.Log("AppDelegate.DidReceiveRemoteNotification: NID: {0}", nid);

                    var waitHandle = new TaskCompletionSource <bool>();
                    if (RemoteNotificationsService.Instance.FireNotificationReceived(new AppNotificationMessage()
                    {
                        ID = nid, Type = AppNotificationType.RemoteNotification, WaitHandle = waitHandle
                    }))
                    {
                        await waitHandle.Task;
                    }

                    Logger.Log("AppDelegate.DidReceiveRemoteNotification: Invoking completion handler");
                    completionHandler?.Invoke(UIBackgroundFetchResult.NewData);
                    break;

                case RemoteNotificationType.Link:
                    AppEvents.LogPushNotificationOpen(userInfo);
                    var lnk = userInfo["lnk"]?.ToString();

                    bool result = false;
                    if (!string.IsNullOrEmpty(lnk))
                    {
                        if (application.ApplicationState == UIApplicationState.Active)
                        {
                            var aps   = userInfo["aps"] as NSDictionary;
                            var alert = aps["alert"];

                            var title   = "Notification";
                            var message = string.Empty;
                            var action  = "Open";

                            if (alert is NSDictionary)
                            {
                                var alertDictionary = alert as NSDictionary;
                                var alertTitle      = alertDictionary["title"]?.ToString();
                                var alertBody       = alertDictionary["body"]?.ToString();
                                var alertAction     = alertDictionary["action-loc-key"]?.ToString();

                                if (!string.IsNullOrEmpty(alertTitle))
                                {
                                    title = alertTitle;
                                }

                                if (!string.IsNullOrEmpty(alertBody))
                                {
                                    message = alertBody;
                                }

                                if (!string.IsNullOrEmpty(alertAction))
                                {
                                    action = alertAction;
                                }
                            }
                            else
                            {
                                message = alert.ToString();
                            }

                            if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(message) && !string.IsNullOrEmpty(action))
                            {
                                var close           = "Close";
                                var closeLocalized  = NSBundle.MainBundle.LocalizedString(close, close, null);
                                var actionLocalized = NSBundle.MainBundle.LocalizedString(action, action, null);
                                var av = new UIAlertView(title, message, null, closeLocalized, actionLocalized);
                                av.Clicked += (s, e) =>
                                {
                                    if (e.ButtonIndex == 1)
                                    {
                                        if (!OpenUrl(application, new NSUrl(lnk), NSBundle.MainBundle.BundleIdentifier, userInfo))
                                        {
                                            Logger.Log("AppDelegate.DidReceiveRemoteNotification: There was an error executing OpenUrl");
                                        }
                                    }
                                };
                                av.Show();
                                result = true;
                            }
                        }
                        else
                        {
                            BeginInvokeOnMainThread(() =>
                            {
                                if (!OpenUrl(application, new NSUrl(lnk), NSBundle.MainBundle.BundleIdentifier, userInfo))
                                {
                                    Logger.Log("AppDelegate.DidReceiveRemoteNotification: There was an error executing OpenUrl");
                                }
                            });
                            result = true;
                        }
                    }

                    completionHandler?.Invoke(result ? UIBackgroundFetchResult.NewData : UIBackgroundFetchResult.NoData);
                    break;

                case RemoteNotificationType.Card:
                    AppEvents.LogPushNotificationOpen(userInfo);

#if __FBNOTIFICATIONS__
                    FBNotificationsBindings.FBNotificationsManager.SharedManager()
                    .PresentPushCardForRemoteNotificationPayload(userInfo,
                                                                 null,
                                                                 (c, e) =>
                    {
                        if (e != null)
                        {
                            Logger.Log("AppDelegate.DidReceiveRemoteNotification: There was an error displaying Facebook advertising notification");
                            completionHandler?.Invoke(UIBackgroundFetchResult.Failed);
                            Logger.Log("AppDelegate.DidReceiveRemoteNotification: Invoking completion handler");
                        }
                        else
                        {
                            Logger.Log("AppDelegate.DidReceiveRemoteNotification: Facebook advertising notification displayed successfully");
                            completionHandler?.Invoke(UIBackgroundFetchResult.NewData);
                            Logger.Log("AppDelegate.DidReceiveRemoteNotification: Invoking completion handler");
                        }
                    }
                                                                 );
#else
                    Logger.Log("AppDelegate.DidReceiveRemoteNotification: Invoking completion handler");
                    completionHandler?.Invoke(UIBackgroundFetchResult.NoData);
#endif
                    break;

                default:
                    Logger.Log("AppDelegate.DidReceiveRemoteNotification: Unrecognized notification type");
                    completionHandler?.Invoke(UIBackgroundFetchResult.NoData);
                    break;
                }
            }
            catch (Exception ex)
            {
                Logger.Log("ERROR: AppDelegate.DidReceiveRemoteNotification: " + ex);
                completionHandler?.Invoke(UIBackgroundFetchResult.Failed);
            }

            if (backgroundTaskId != 0)
            {
                UIApplication.SharedApplication.EndBackgroundTask(backgroundTaskId);
                backgroundTaskId = 0;
            }

            Logger.Log("AppDelegate.DidReceiveRemoteNotification: Event dispatch complete");
        }