Unity Pro Tip #2: iOS 8 Push Notifications

Now iOS 8 is out, we have to release updates for our Unity apps. Here at Cheese Burgames, we are preparing the first update for our first game, Echoes: Deep-sea Exploration. But during our pre-release tests, I noticed that remote notifications were not working anymore. Why that?

The answer is in the deprecation of the UIApplication’s registerForRemoteNotificationTypes: method in iOS 8.

Internally, NotificationServices.RegisterForRemoteNotificationTypes calls it. And now, it does not produce any result. Instead, you will see this message in the Xcode debug console:

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

Nothing less, nothing more. And as of the currently available version of Unity (4.5.4f1) or in patch releases, there is no workaround. But we have a solution!

The Quick Fix

To resolve this issue, you have to use an iOS plugin and a script.

First, create a file called CBG_NotificationServices.mm in your Assets/Plugins/iOS folder (create this folder if it does not exist yet). Here is the content of this file:

#import <UIKit/UIKit.h>

extern "C" {
    void CBG_RegisterForRemoteNotifications(int types) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:Nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}

After that, create a C# script called CBGNotificationServices.cs in your Assets folder (or anywhere else you store your player scripts) and fill it with these lines:

using UnityEngine;
using System.Runtime.InteropServices;

public class CBGNotificationServices {

#if UNITY_IOS
    [DllImport ("__Internal")]
    private static extern void CBG_RegisterForRemoteNotifications(int types);
 
    public static void RegisterForRemoteNotificationsTypes(RemoteNotificationType types) {
        if (!Application.isEditor) {
            string sysInfo = SystemInfo.operatingSystem;
            sysInfo = sysInfo.Replace("iPhone OS ", "");
            string[] chunks = sysInfo.Split('.');
            int majorVersion = int.Parse(chunks[0]);
            if (majorVersion >= 8)
                CBG_RegisterForRemoteNotifications((int) types);
            else
                NotificationServices.RegisterForRemoteNotificationsTypes(types);
        }
    }
#endif
 
}

Now, anywhere you are using NotificationServices.RegisterForRemoteNotificationsTypes(), you can simply replace it by:

CBGNotificationServices.RegisterForRemoteNotificationsTypes(/* Your remote notification types here */);

And you’re done!

This solution is a temporary one. It is working in our app but we recommend very careful testing before using it in a release. The bug has been reported and I hope the Unity team will soon provide a permanent fix.

Unity Pro Tip: Hierarchy Alphabetical Sort

If you are an experienced Unity user, you may have noticed that the sort order of the hierarchy window has changed in Unity 4.5, which now uses what’s called a Transform Sort. In fact, changes are even deeper than that as Unity Technologies offers now the option to customize this sort order. This can be made through editor scripts, providing each one custom sort type. First, if you want to revert to the previous behavior, add this script to an Editor folder in your Assets:

using UnityEngine;
using UnityEditor;

public class AlphaSortAlphabeticalSort { }

UnityEditor.AlphabeticalSort is a built-in class. I wonder why this sort is not available out of the box. But, that’s not the point here. Once this script is compiled by Unity, you should see a new drop-down list in the Hierarchy window, allowing you to choose the sort order you want to use. Unity - Hierarchy Alpha Sort That is only the first step in this new world of custom sort types. If you want to provide your own unique sort type, you can dive into the UnityEditor.BaseHierarchySort class documentation to learn more.