Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Xamarin.ios push notification is not working

$
0
0

Hi, I am using MS Azure Notification Hub for implementing Push Notification, Its working fine on Android but it's not working on IOS , I am getting a message : "Successfully sent test message. Outcome: {0 passed, 0 failed}. Please see result section for details.
Successfully sent test message. Outcome: {0 passed, 0 failed}. Please see result section for details."

Here is my App.Delegete.cs , please suggest any solution,

    namespace Test.iOS
    {
        // The UIApplicationDelegate for the application. This class is responsible for launching the
        // User Interface of the application, as well as listening (and optionally responding) to
        // application events from iOS.
        [Register("AppDelegate")]
        public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
        {
            public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            {
                global::Xamarin.Forms.Forms.Init ();
                App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
                App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
                LoadApplication(new Test.App());
                CrossBleAdapter.Init(null);
                KeyboardOverlapRenderer.Init();

                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    var authOptions = UserNotifications.UNAuthorizationOptions.Alert | UserNotifications.UNAuthorizationOptions.Badge | UserNotifications.UNAuthorizationOptions.Sound;
                    UserNotifications.UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                    {
                        Console.WriteLine(granted);
                    });
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
                else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
                    UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
                else
                {
                    var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
                }



                return base.FinishedLaunching (app, options);
            }
            public override void WillTerminate(UIApplication uiApplication)
            {
                base.WillTerminate(uiApplication);
            }

            private SBNotificationHub Hub { get; set; }
            public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
            {
                Hub = new SBNotificationHub(Test.Helpers.ApplicationConstants.Constants.ConnectionString, Test.Helpers.ApplicationConstants.Constants.NotificationHubPath);

                Hub.UnregisterAllAsync(deviceToken, (error) => {
                    if (error != null)
                    {
                        Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                        return;
                    }

                    NSSet tags = null; // create tags if you want
                    Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
                        if (errorCallback != null)
                            Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
                    });
                });
            }

            public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
            {
                ProcessNotification(userInfo, false);
            }
            void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
            {
                // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
                if (null != options && options.ContainsKey(new NSString("aps")))
                {
                    //Get the aps dictionary
                    NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;

                    string alert = string.Empty;

                    if (aps.ContainsKey(new NSString("alert")))
                        alert = (aps[new NSString("alert")] as NSString).ToString();

                    if (!fromFinishedLaunching)
                    {
                        //Manually show an alert
                        if (!string.IsNullOrEmpty(alert))
                        {
                            UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
                            avAlert.Show();
                        }
                    }
                }
            }
        }
    }

Thanks in advance


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>