/**
 Push
 Notifications  */

Go back to Xamarin Getting Started Guide

Push Notifications (Xarmin - iOS)

6. Enable Remote Notifications in Xamarin IDE Project

You can either turn Remote Notifications on either from the Xamarin IDE or by editing the app's property list (usually Info.plist) file depending on you preference.

Option 1: Using Xamarin IDE

  • In Xamarin IDE, open your plist file.
  • Open Background Modes, Enable Background Modes and Remote Notifications by clicking on the corresponding check marks.

    Remote Notifications

Option 2: Editing app's property list (usually Info.plist) file directly

Open your Info.plist file and add following lines

    <key>UIBackgroundModes</key>
    <array>
      <string>remote-notification</string>
    </array>

7. Enable your app for Push Notifications

Generate a build for iOS before proceeding.

Enable Remote Notifications

Open info.plist and enable background mode and check Remote notification option

Add the following snippet inside FinishedLaunching lifecycle callback of the App Delegate

// Override point for customization after application launch.
// If not required for your application you can safely delete this method
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
   var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
          UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());

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

Override RegisteredForRemoteNotifications lifecycle callback of the App Delegate and add the following snippet

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
  // Get current device token
  var DeviceToken = deviceToken.Description;
  if (!string.IsNullOrWhiteSpace(DeviceToken))
  {
    DeviceToken = DeviceToken.Trim('<').Trim('>');
  }

  // Get previous device token
  //var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

  Pyze.SetRemoteNotificationDeviceToken(deviceToken);

  // Save new device token 
  NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}

Override DidReceiveRemoteNotification lifecycle callback of the App Delegate and add the following snippet

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler)
{
  Pyze.ProcessReceivedRemoteNotification(userInfo);
}



See App Store Review Guidelines for Push Notifications

To print, click on expand all sections and use your browser's print function. The images will print in printer-friendly size.


Go back to Xamarin Developer Center.


About Pyze

Pyze Growth Intelligence® is a transformative SaaS platform that enables mobile and web app businesses to automate engagement, retention and growth of their users. Signup at Pyze, install and initialize the Pyze SDK and watch your app grow.