iOS

iOS

You can see it in this tutorial video or read the instructions below:

To start with the iOS configuration, make sure you have OK these points:

  • Import the plugin using NuGet as mentioned in section [import the plugin ] (# import-the-plugin)
  • Set the Notification Center Delegate as explained in the official guide.

🚧

Note

Nota: For the SDK to work correctly the FinishedLaunching method must be called from the AppDelegate class, before the call to the LoadApplication method occurs.

The AppDelegate class should look like this:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    Forms.Init();
    DependencyService.Register<Com.Indigitall.Xamarin.iOS.Indigitall>();

    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        // iOS 10 or later
        var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
        UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
        {
            if (granted)
            {
                InvokeOnMainThread(() =>
                {
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                });
            }
        });

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.Current.Delegate = new Com.Indigitall.Xamarin.iOS.UserNotificationCenterDelegate();

    }
    else
    {
        // iOS 9 or before
        var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
        var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
        UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
    }
    ...
    UNUserNotificationCenter.Current.Delegate = new Com.Indigitall.Xamarin.iOS.UserNotificationCenterDelegate();
    ...
    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
override public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    IndigitallXamarin.Indigitall.SetDeviceToken(deviceToken, (device) =>
    {
        Console.WriteLine("NewUserRegistered: " + device.DeviceID);
    });
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public virtual void DidReceiveNotificationResponse(UserNotifications.UNUserNotificationCenter center, UserNotifications.UNNotificationResponse response, Action completionHandler)
    {
        IndigitallXamarin.Indigitall.HandleWithResponse(response);
    }

//@DEPRECATED
[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
override public void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    IndigitallXamarin.Indigitall.HandleWithNotification(userInfo, null);
}

//@DEPRECATED
[Export("application:handleActionWithIdentifier:forRemoteNotification:withResponseInfo:completionHandler:")]
override public void HandleAction(UIApplication application, string actionIdentifier, NSDictionary remoteNotificationInfo, NSDictionary responseInfo, Action completionHandler)
{
    IndigitallXamarin.Indigitall.HandleWithNotification(remoteNotificationInfo, actionIdentifier);
}

Notification Service Extension

Since the release of iOS 10, apps can manage rich push notifications, that is, with images, gif, video, buttons, etc.
In order to use these features, your app needs to implement the Notification Service Extension.

    1. Add a new Notification Service Extension project to your solution
    1. Add the dependency Com.Indigitall.Xamarin from NuGet
    1. Reference the target extension in your iOS project
    1. Once you've created the extension, a new file is created within the project. It's the NotificationService. Replace its content with the following lines:
using System;
using Foundation;
using Com.Indigitall.Xamarin.iOS;

namespace Notification
{
    [Register("NotificationService")]
    public class NotificationService : UNNotificationServiceExtension
    {
        Action<UNNotificationContent> ContentHandler { get; set; }
        UNMutableNotificationContent BestAttemptContent { get; set; }
        UNNotificationRequest _request { get; set; }

        protected NotificationService(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
        {
            ContentHandler = contentHandler;
            BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();
            _request = request;

            // Modify the notification content here...
            IndigitallXamarin.Indigitall.DidReceiveNotificationRequest(_request, ContentHandler);
        }

        public override void TimeWillExpire()
        {
            // Called just before the extension will be terminated by the system.
            // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
            if (ContentHandler != null && BestAttemptContent != null)
            {
                IndigitallXamarin.Indigitall.ServiceExtensionTimeWillExpire(BestAttemptContent, ContentHandler);
            }

        }
    }
}