- Home /
Create "Notification Service Extension" from post build script
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: https://documentation.onesignal.com/docs/unity-sdk-setup
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.
Hi! Sorry for late answer. That Unity 5 tag was a mistake, it should be Unity 2017.1. And we havent found a solution yet. Working on it, will keep you updated. Have you tried? Any luck?
I'm in the process of upgrading to 2017.1.1 to see if I can make it work. We were still on unity 5.6.3 because we saw many graphics glitches in our 2D game when upgrading to 2017.1, hope they have fixed it now in 2017.1.1.
Answer by andersemil · Oct 11, 2017 at 10:14 AM
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.
Again, this code snippet does not create the extension, it merely includes the extension in the Unity Xcode project and sets up the target. We use this exact same code with Unity 2018 and Xcode 10 today.
What do you mean by merely including the extension? I have added correct Info.plist and source files to this code, what else have you done to get a fully working extension?
Answer by ashwin_dinesh · May 06, 2019 at 01:36 PM
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" ins$$anonymous$$d ;) for next time.