iOS Push Configuration
campaign

Notice

Microsoft is replacing Xamarin with .NET MAUI, and the support for the latest release of Xamarin is till November 2023. MoEngage will stop providing updates for the MoEngage Xamarin SDK due to the deprecation. Deprecation impacts Xamarin In-App Messaging and will stop working after June 2022. All other features will continue to work in the Xamarin SDK until November 2023.

For configuring Push Notification in iOS platform do the following:

Upload APNS certificate to Dashboard

First, you will have to create an APNS certificate and upload in the dashboard to be able to send push notifications in iOS. Follow the steps below to do that :

Follow the links on each step to complete it.

Project Settings Changes

1. Enable Push Notifications Entitlement:

Go to your iOS Project's Entitlements.plist file and do the following:

  1. Enable Push Notifications Capability.
  2. Enable App Groups and set/create an app group ID(The name of your app group should be group.{your_bundle_id}.MoEngage)
info

App Groups

MoEngage SDK uses App Group IDs to share info between iOS App Target and Notification Service Extension target.

1b33ebe-Screenshot_2019-12-22_at_19.41.03.png

2. Enable Remote Notifications Background Mode

Go to your iOS Project's Info.plist file and Enable Remote Notifications Background Mode as shown below:

0ecdb21-Screenshot_2019-12-22_at_19.42.14.png

Push Registration

  1. First, you will have to set the UNUserNotificationCenter delegate in FinishedLaunching() method of AppDelegate file. This is for getting the callbacks when notifications are clicked. And make sure to set it before initializing the SDK.
    C#
    using UserNotifications;
    using MoEngageXamarin.iOS;
      
    namespace Sampleapp.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();
                LoadApplication(new App());
    					
              //Make sure to set UNUserNotificationCenter delegate before Initializing the SDK.
               	UNUserNotificationCenter.Current.Delegate = new MyUNUserNotificationCenterDelegate();
              
    #if DEBUG
                MoEInitializer.InitializeDevWithAppID("Your App ID", app, options);
    #else
                MoEInitializer.InitializeProdWithAppID("Your App ID", app, options);
    #endif
    
                return base.FinishedLaunching(app, options);
            }
  2. Provide the App Group ID set in the Entitlements.plist file in FinishedLaunching method of App Delegate file:
    C#
    MoEngage.SetAppGroupID(<AppGroupID Set in Entitlement.plist>);
  3. Call RegisterForRemoteNotificationWithCategories() method for registering for Push Notification.
    C#
    // Call RegisterForRemoteNotificationWithCategories method to register for Push Notification
    MoEngage.SharedInstance().RegisterForRemoteNotificationWithCategories(null, (NSObject)UNUserNotificationCenter.Current.Delegate);
  4. Callback for token registration
    On successful registration of push token, you will receive RegisteredForRemoteNotifications() callback in your AppDelegate class of iOS project, call SetPushToken() of MoEngage SDK here as shown below:
    C#
    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
      MoEngage.SharedInstance().SetPushToken(deviceToken);
    }
  5. Callbacks on receiving and clicking the notifications
    You would receive the following callbacks on receiving(only when app in foreground) and clicking Push Notification. Make sure to call MoEngage SDKs UserNotificationCenter() method in DidReceiveNotificationResponse() method as shown below:
    C#
    public class MyUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
        {
            // Callback on receiving notification when app in foreground
            public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
            {
                completionHandler(UNNotificationPresentationOptions.Alert);
            }
    
            // Callback on clicking notification
            public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
            {
                MoEngage.SharedInstance().UserNotificationCenter(center, response);
                completionHandler();
            }
        }

Rich Notification Support

In order to support rich notifications in your app, you will have to make use of Notification Service Extension for your iOS app. Notification Service Extension for your iOS app can be used for the following:

  1. Add media support in Notifications: Post iOS10 Apple has given us the ability to add images, gifs, audio, and video files to the notifications and this can be done using the Notification Service Extension.

  2. For supporting Inbox Feature: Notification Service Extension is also used to save the received notifications which can later be shown in the App Inbox.

  3. For Updating the Notification Badge count: MoEngage makes use of the extension to update the notification badge count and doesn't send badge in the notification payload.

  4. For Tracking Notification Impression: We can track if a Notification is received by the device using the Notification Service Extension.

Set up Notification Service Extension in Xamarin

Follow the below steps to set up Notification Service Extension in Xamarin:
  1. Right-click on the main solution project and click on Add > Add New Project. Post this select iOS Extension > Notification Service Extension while choosing a project template.9e7c9c3-Screenshot_2019-12-22_at_20.01.31.png
  2. Set the extension name and set the Project to iOS App Project.0841422-Screenshot_2019-12-22_at_20.01.55.png
  3. Review the changes in the third step and click on the Create button.d11a4af-Screenshot_2019-12-22_at_20.02.15.png
  4. Make sure to set the deployment target greater than iOS 10. This is because Notification Service Extension was introduced by Apple from iOS 10.e4f10a5-Screenshot_2019-12-22_at_19.40.16.png
  5. Go to Entitlements.plist file in Extension project and enable the following:
    a) First Enable Push Notification Capability
    b) Then Enable App Groups and set the App Group ID to the same ID which was set in the App's Entitlement file.b7b3c61-Screenshot_2019-12-22_at_19.39.19.png
  6. Now add com.moengage.ios.RichNotification NuGet package to Notification Service Extension Project.ab4522f-Screenshot_2019-12-22_at_19.37.31.png
  7. Code Implementation:
    Use MoEngageXamarin.iOS namespace and call SetAppGroupID and HandleRichNotificationRequest methods of MORichNotification class as shown below
    C#
    using System;
    using Foundation;
    using UIKit;
    using UserNotifications;
    using MoEngageXamarin.iOS;
    
    namespace NotificationServices
    {
        [Register("NotificationService")]
        public class NotificationService : UNNotificationServiceExtension
        {
            Action<UNNotificationContent> ContentHandler { get; set; }
            UNMutableNotificationContent BestAttemptContent { get; set; }
    
            protected NotificationService(IntPtr handle) : base(handle)
            {}
    
            public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
            {
                ContentHandler = contentHandler;
                MORichNotification.SetAppGroupID(<AppGroupID Set in Entitlement.plist>);
                MORichNotification.HandleRichNotificationRequest(request, contentHandler);
            }
    
            public override void TimeWillExpire()
            {
                ContentHandler(BestAttemptContent);
            }
        }
    }
warning

Rich Notification Media Limitations:

  • Refer to the following link to know about the size and format limitation for attachments(media) supported in Rich Notifications.

  • Http URL's aren't supported in iOS10 unless explicitly specified in the plist. You will have include App Transport Security Settings Dictionary in your Notification Service Extension's Info.plist and inside this set Allow Arbitrary Loads to YES.

check_circle

Image Guidelines

  • File Size: The maximum filesize for image attachments can be 10MB.
  • Dimensions: The maximum possible dimensions are 1038 x 1038 pixels. It can be anything smaller than 1038 pixels.
  • Landscape vs Portrait: iOS supports both the orientations but we recommend using images that have a landscape orientation this is because depending on the dimensions, portrait images may look too tall.

Geofence Campaigns

To support geofence based campaigns in iOS, do the following:

  1. Add com.moengage.ios.geofence package to your iOS Projecte39a010-Screenshot_2019-12-22_at_19.37.46.png
  2. Call StartGeofenceWithManager post obtaining user's location to initiate the Geofence module:
    C#
    MOGeofenceHandler.SharedInstance().StartGeofenceWithManager(manager, locationCoordinates);
Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?