Xamarin Forms iOS Remote Notifications Not Displaying

Published

Remote Notifications are not displaying on the device.

  • We are using iOS 11.2 and Twilio.
  • We have generated the APN in Apple Developer Portal and exported the
    certificate and key into Twilio.
  • Twilio says the message is "sent," but it never displays on the
    device.

The goal is to send a message with a simple header and body text, and have that display as a remote push notification on the device.

The Xamarin documentation seems incomplete, and I cannot find clear instructions on how to handle displaying the notification. I have looked at the Xamarin samples, but they mostly cover local notifications.

Questions are below in the comments. What is missing?

using Foundation;
using UserNotifications;
using UIKit;

namespace MyNotifications.iOS
{
    
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {

        protected UIWindow window;
        protected string deviceToken = string.Empty;
        public string DeviceToken { get { return deviceToken; } }

        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());  

            // check for a notification while running
            if (options != null)
            {

                if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
                {
                    NSDictionary remoteNotification = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
                    if (remoteNotification != null)
                    {
                        //1) is this necessary to handle??? if so, how to display? what are the properties from the remoteNotification object that contain the text?
                    }
                }


            }

            //this prompts for permissions, which are set
            if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
                                               UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
                                           );

                app.RegisterUserNotificationSettings(notificationSettings);
                app.RegisterForRemoteNotifications();
            }
            else
            {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }

            UNUserNotificationCenter.Current.GetNotificationSettings((settings) =>
            {
                var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled);
            });

            // Request notification permissions from the user
            UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
            {
                // 2) how do we handle this??? what comes next?
            });

            return base.FinishedLaunching(app, options);
        }

    
        // 3) does this override need to do anything???
        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            // 4) should all of these trigger a notification? does that have to happen manually?
            if (application.ApplicationState == UIApplicationState.Active)
            {
                ProcessPushNotification(userInfo, true);
            }
            else if (application.ApplicationState == UIApplicationState.Background)
            {
                ProcessPushNotification(userInfo, true);
            }
            else if (application.ApplicationState == UIApplicationState.Inactive)
            {
                ProcessPushNotification(userInfo, true);
            }
        }

        protected void ProcessPushNotification(NSDictionary userInfo, bool isAppAlreadyRunning)
        {
            if (userInfo == null) return;
            if (isAppAlreadyRunning)
            {
                // 5) do we need to generate our own view???
            }
            else
            {
                // 6) how to handle in the background???
            }

        }

        // APNS background
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            deviceToken = deviceToken.ToString();
        }

        // Handle errors and offline
        public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
        {
            // 7) what to do here???
        }
    }
}

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , , , ,

Answers

Microsoft Docs has a few guides on how to deal with push notifications on both platforms, iOS and Android. Most of them are sending notifications from an Azure Notifications Hub but in this case, it shouldn’t make any difference, because your question is about displaying push notifications, and not sending them.

The guide on how to send and receive notifications in a Xamarin.Forms app gives you an idea of the complete end-to-end setup. There are also two slightly different guides with a focus on Azure here and here

And for rendering messages on iOS, if the app is backgrounded, the notification is rendered by iOS without any custom code so if the app is properly configured and signed, you should be able to see a push notifications without any additional client-side code, just make sure you test on a device (sims doesn’t support pushes) and valid profile with pushes enabled.


Prof. Zella Kemmer IV

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq