Unity Pro Tip #2: iOS 8 Push Notifications

UPDATE: This bug has been fixed in Unity 4.5.5p1 (released October 16th, 2014) and in Unity 4.6.0p1 (released december 4th, 2014).

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 {

    [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);

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.