Warm tip: This article is reproduced from stackoverflow.com, please click
apple-push-notifications azure xamarin.forms xamarin.ios

Xamarin Forms IOS with Azure Notification Hub

发布于 2020-05-04 01:37:29

I know this question is asked multiple times. So bear with me here.

I am trying to implement Azure Notification Hub with My Android / iOS clients using Xamarin Forms.

For Android its going ok but for iOS I am not receiving any Notifications.

Here is a checklist of what I have done for iOS

  1. Generated the Certificates and Profiles
  2. Registered the Certificate with a MacOS
  3. Exported the p12 file and Imported in Azure Hub
  4. I also added the permissions in Info.plist (background remote notification)
  5. No Changes in the Entitlements.plist file
  6. I am able to get the Device token from the APN Service
  7. I am able to register onto the Azure Hub after getting the Token

But I am unable to receive any notification and also none of the functions are called(DidRegisterUserNotificationSettings,WillPresentNotification,ReceivedRemoteNotification,DidRegisterUserNotificationSettings,FailedToRegisterForRemoteNotifications,DidReceiveRemoteNotification)

But RegisteredForRemoteNotifications is called

I also tested the devicetoken with or without removing the '<> ' from it but no success.

What may be the issue?

Here is my code

 public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            GlobalVariables.Instance.PnsToken = deviceToken.Description
                                 .Replace("<", string.Empty)
                                 .Replace(">", string.Empty)
                                 .Replace(" ", string.Empty)
                                 .ToUpper();
            RegisteredForHub(application, deviceToken);
        }

        public void RegisteredForHub(UIApplication application, NSData deviceToken)
        {
            string pnsHandle = deviceToken.Description
                                 .Replace("<", string.Empty)
                                 .Replace(">", string.Empty)
                                 .Replace(" ", string.Empty)
                                 .ToUpper();
            _hub = new SBNotificationHub(Constant.ListenConnectionString, Constant.NotificationHubName);

            _hub.UnregisterNative((error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister Native {error}");
                }
            });
            _hub.UnregisterTemplate("defaultTemplate",(error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister Template {error}");
                }
            });
            // 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(Constant.SubscribeTags);
                _hub.RegisterNative(pnsHandle, 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", Constant.ApnTemplateBody, templateExpiration,
                //    tags, (errorCallback) =>
                //    {
                //        if (errorCallback != null)
                //        {
                //            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                //        }
                //    });
            });
Questioner
Noob Coder
Viewed
31
Noob Coder 2020-02-24 18:07

After alot of trials and errors and so many hours googling. I found out the issue was with the Azure.

I had uploaded the the correct certificate but the password was one character of due to which no notifications were sent.

Azure should prompt the user for APNs if the user has entered the wrong password for its Certificate file.