"Create ""Notification Service Extension"" from post build script" (195493)

Hi!
We are using a postbuild-script to modify our xcode project.

Works great, but now we need your help. We are trying to add a “Notification Service Extension”-target from code (see picture).

We can add a new xcode target by using PBXProject.AddAppExtension, but with this we only get a “normal” target, not a specific “Notification Service Extension”-template-one..

Reason why we want this done by code is that we are trying to get OneSignal to work with Unity Cloud Build. We have step 1 ready and working from our script, but struggling with the second part in the setup guide found here: Unity SDK setup - OneSignal

Any input would be very nice. Thanks!

@fox Were you ever able to find a solution? I can't even find the AddAppExtension method on PBXProject. According to the docs, this is only available in 2017.1 but your post is tagged with Unity 5? Completely lost.

–

2 Answers

2

The type of extension is determined by the NSExtensionPointIdentifier entry in your extension’s Info.plist.
I managed to automate the creation of a notification service extension target and setting of all the annoying things that need to be set.
Below is the code I’m using

    [PostProcessBuild]
    public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
    {
        if(target == BuildTarget.iOS)
        {
            var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
            PBXProject proj = new PBXProject ();
            proj.ReadFromFile (projPath);
            string targetGUID = proj.TargetGuidByName ("Unity-iPhone");

            var plistPath = pathToBuiltProject + "/Info.plist";
            PlistDocument plist = new PlistDocument();
			plist.ReadFromFile (plistPath);

			var pathToNotificationService = /* PATH TO YOUR NOTIFICATION SERVICE SOURCEFILES */;
			var notificationServicePlistPath = pathToNotificationService + "/Info.plist";
			PlistDocument notificationServicePlist = new PlistDocument();
			notificationServicePlist.ReadFromFile (notificationServicePlistPath);
			notificationServicePlist.root.SetString ("CFBundleShortVersionString", PlayerSettings.bundleVersion);
			notificationServicePlist.root.SetString ("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString ());
			var notificationServiceTarget = PBXProjectExtensions.AddAppExtension (proj, targetGUID, "notificationservice", PlayerSettings.GetApplicationIdentifier (BuildTargetGroup.iOS) + ".notificationservice", notificationServicePlistPath);
			proj.AddFileToBuild (notificationServiceTarget, proj.AddFile (pathToNotificationService + "/NotificationService.h", "NotificationService/NotificationService.h"));
			proj.AddFileToBuild (notificationServiceTarget, proj.AddFile (pathToNotificationService + "/NotificationService.m", "NotificationService/NotificationService.m"));
			proj.AddFrameworkToProject (notificationServiceTarget, "NotificationCenter.framework", true);
			proj.AddFrameworkToProject (notificationServiceTarget, "UserNotifications.framework", true);
			proj.SetBuildProperty (notificationServiceTarget, "ARCHS", "$(ARCHS_STANDARD)");
			proj.SetBuildProperty (notificationServiceTarget, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);
			notificationServicePlist.WriteToFile (notificationServicePlistPath);

            proj.WriteToFile (projPath);
			plist.WriteToFile (plistPath);
        }
    }

I tried this code snippet in Unity 2018, it creates a Notification Service Extension, but it did not work. Whereas when I create the same extension from Xcode, it works perfectly. Could you please confirm that this works with Unity 2018 and Xcode 10.

–

Where are you setting value for NSExtensionPointIdentifier?

You need to create the service from template in Xcode first and then link to it using my example above. Btw, you've posted your comment as an answer, you should have clicked "Add comment" instead ;) for next time.

–