Beispiel #1
0
        public static void OnPostprocessBuildHandler(BuildTarget buildTarget, string path)
        {
            if (buildTarget == BuildTarget.iOS)
            {
                var projPath = PBXProject.GetPBXProjectPath(path);
                var proj     = new PBXProject();
                proj.ReadFromFile(projPath);

#if UNITY_2019_3_OR_NEWER
                var targetGUID = proj.GetUnityFrameworkTargetGuid();
 #else
                var targetGUID = proj.TargetGuidByName("Unity-iPhone");
 #endif
                var notificationServicePlistPath = PathToNotificationService + "/Info.plist";

                var notificationServicePlist = new PlistDocument();
                notificationServicePlist.ReadFromFile(notificationServicePlistPath);
                notificationServicePlist.root.SetString("CFBundleShortVersionString", PlayerSettings.bundleVersion);
                notificationServicePlist.root.SetString("CFBundleVersion", PlayerSettings.iOS.buildNumber);
                notificationServicePlist.root.SetString("CFBundleDisplayName", PlayerSettings.iOS.applicationDisplayName);

                notificationServicePlist.WriteToFile(notificationServicePlistPath);

                var notificationServiceTarget = PBXProjectExtensions.AddAppExtension(proj, targetGUID, "notificationservice", PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + ".bknotificationservice", notificationServicePlistPath);

                proj.AddHeadersBuildPhase(notificationServiceTarget);

                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, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");
                proj.SetBuildProperty(notificationServiceTarget, "TARGETED_DEVICE_FAMILY", "1,2");

                proj.WriteToFile(projPath);
            }
        }
    public static void CreateRichPushNotificationTarget(BuildTarget buildTarget, string buildOutputPath)
    {
        iOSConfiguration config = iOSConfiguration.Load();

        if (!config.enableRichPushNotifications)
        {
            return;
        }

        string     xcodeProjectPath = PBXProject.GetPBXProjectPath(buildOutputPath);
        PBXProject project          = new PBXProject();

        project.ReadFromFile(xcodeProjectPath);

        string guidOfInitialTarget = GetTargetGuid(project);

        string        pathToInfoPlist      = Path.Combine(buildOutputPath, PATH_TO_INFO_PLIST_INSIDE_TARGET);
        PlistDocument mainProjectInfoPlist = new PlistDocument();

        mainProjectInfoPlist.ReadFromFile(pathToInfoPlist);
        PlistElementArray array = mainProjectInfoPlist.root.CreateArray("UIBackgroundModes");

        array.AddString("remote-notification");
        mainProjectInfoPlist.WriteToFile(pathToInfoPlist);

        string bundleIdentifierForNotificationService = config.pushNotificationServiceExtensionIdentifier;
        int    indexOfLastIdentifierSection           = bundleIdentifierForNotificationService.LastIndexOf('.') + 1;
        string displayName = bundleIdentifierForNotificationService.Substring(indexOfLastIdentifierSection);

        string pathToNotificationServiceImplementation = Path.Combine(buildOutputPath, displayName);

        if (!Directory.Exists(pathToNotificationServiceImplementation))
        {
            Directory.CreateDirectory(pathToNotificationServiceImplementation);
        }

        PlistDocument notificationServicePlist = new PlistDocument();
        string        plistTemplatePath        = Path.Combine(GetPathToSourceDirectory(), "Info.plist");

        notificationServicePlist.ReadFromFile(plistTemplatePath);
        notificationServicePlist.root.SetString("CFBundleDisplayName", displayName);
        notificationServicePlist.root.SetString("CFBundleIdentifier", bundleIdentifierForNotificationService);
        notificationServicePlist.root.SetString("CFBundleShortVersionString", PlayerSettings.bundleVersion);
        notificationServicePlist.root.SetString("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString());

        string pathToNotificationServicePlist = Path.Combine(pathToNotificationServiceImplementation, PATH_TO_INFO_PLIST_INSIDE_TARGET);

        notificationServicePlist.WriteToFile(pathToNotificationServicePlist);

        string guidOfExtension = PBXProjectExtensions.AddAppExtension(
            project, guidOfInitialTarget,
            displayName,
            PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + ".notificationservice",
            pathToNotificationServicePlist
            );
        string buildPhaseId = project.AddSourcesBuildPhase(guidOfExtension);

        AddSourceFileToProject(
            project,
            "NotificationService.h",
            displayName,
            guidOfExtension,
            buildPhaseId,
            pathToNotificationServiceImplementation
            );
        AddSourceFileToProject(
            project,
            "NotificationService.m",
            displayName,
            guidOfExtension,
            buildPhaseId,
            pathToNotificationServiceImplementation
            );
        AddFileToProject(
            project,
            pathToNotificationServicePlist,
            "Info.plist",
            displayName,
            guidOfExtension,
            buildPhaseId
            );

        project.AddFrameworkToProject(guidOfExtension, "NotificationCenter.framework", true);
        project.AddFrameworkToProject(guidOfExtension, "UserNotifications.framework", true);
        project.SetBuildProperty(guidOfExtension, "ARCHS", "$(ARCHS_STANDARD");
        project.SetBuildProperty(guidOfExtension, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);

        string[] copyableProperties =
        {
            "IPHONEOS_DEPLOYMENT_TARGET",
            "TARGETED_DEVICE_FAMILY"
        };
        foreach (string copyableProperty in copyableProperties)
        {
            string originalBuildProperty = project.GetBuildPropertyForAnyConfig(guidOfInitialTarget, copyableProperty);
            project.SetBuildProperty(guidOfExtension, copyableProperty, originalBuildProperty);
        }

        project.WriteToFile(xcodeProjectPath);
    }
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {
        string     projectPath = PBXProject.GetPBXProjectPath(path);
        PBXProject project     = new PBXProject();

        project.ReadFromString(File.ReadAllText(projectPath));
        string targetName = PBXProject.GetUnityTargetName();
        string targetGUID = project.TargetGuidByName(targetName);

        // UserNotifications.framework is required by libOneSignal.a
        project.AddFrameworkToProject(targetGUID, "UserNotifications.framework", false);

        var extensionTargetName       = "OneSignalNotificationServiceExtension";
        var pathToNotificationService = path + "/" + extensionTargetName;

        Directory.CreateDirectory(pathToNotificationService);

        var notificationServicePlistPath = pathToNotificationService + "/Info.plist";

        PlistDocument notificationServicePlist = new PlistDocument();

        notificationServicePlist.ReadFromFile("Assets/OneSignal/Platforms/iOS/Info.plist");
        notificationServicePlist.root.SetString("CFBundleShortVersionString", PlayerSettings.bundleVersion);
        notificationServicePlist.root.SetString("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString());

        var notificationServiceTarget = PBXProjectExtensions.AddAppExtension(project, targetGUID, extensionTargetName, PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + "." + extensionTargetName, notificationServicePlistPath);

        var sourceDestination = extensionTargetName + "/NotificationService";

        project.AddFileToBuild(notificationServiceTarget, project.AddFile(sourceDestination + ".h", sourceDestination + ".h", PBXSourceTree.Source));
        project.AddFileToBuild(notificationServiceTarget, project.AddFile(sourceDestination + ".m", sourceDestination + ".m", PBXSourceTree.Source));

        var frameworks = new string[] { "NotificationCenter.framework", "UserNotifications.framework", "UIKit.framework", "SystemConfiguration.framework" };

        foreach (string framework in frameworks)
        {
            project.AddFrameworkToProject(notificationServiceTarget, framework, true);
        }

        //makes it so that the extension target is Universal (not just iPhone) and has an iOS 10 deployment target
        project.SetBuildProperty(notificationServiceTarget, "TARGETED_DEVICE_FAMILY", "1,2");
        project.SetBuildProperty(notificationServiceTarget, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");

        project.SetBuildProperty(notificationServiceTarget, "ARCHS", "$(ARCHS_STANDARD)");
        project.SetBuildProperty(notificationServiceTarget, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);

        notificationServicePlist.WriteToFile(notificationServicePlistPath);

        FileUtil.CopyFileOrDirectory("Assets/OneSignal/Platforms/iOS/NotificationService.h", path + "/" + sourceDestination + ".h");
        FileUtil.CopyFileOrDirectory("Assets/OneSignal/Platforms/iOS/NotificationService.m", path + "/" + sourceDestination + ".m");

        project.WriteToFile(projectPath);

        //add libOneSignal.a to the OneSignalNotificationServiceExtension target
        string contents = File.ReadAllText(projectPath);

        //this method only modifies the PBXProject string passed in (contents).
        //after this method finishes, we must write the contents string to disk
        InsertStaticFrameworkIntoTargetBuildPhaseFrameworks("libOneSignal", "CD84C25F20742FAB0035D524", notificationServiceTarget, ref contents, project);
        File.WriteAllText(projectPath, contents);

        // enable the Notifications capability in the main app target
        project.ReadFromString(contents);
        var entitlementPath = path + "/" + targetName + "/" + targetName + ".entitlements";

        PlistDocument entitlements = new PlistDocument();

        entitlements.root.SetString("aps-environment", "development");
        entitlements.WriteToFile(entitlementPath);

        // Copy the entitlement file to the xcode project
        var entitlementFileName = Path.GetFileName(entitlementPath);
        var unityTarget         = PBXProject.GetUnityTargetName();
        var relativeDestination = unityTarget + "/" + entitlementFileName;

        // Add the pbx configs to include the entitlements files on the project
        project.AddFile(relativeDestination, entitlementFileName);
        project.AddBuildProperty(targetGUID, "CODE_SIGN_ENTITLEMENTS", relativeDestination);

        // Add push notifications as a capability on the target
        project.AddBuildProperty(targetGUID, "SystemCapabilities", "{com.apple.Push = {enabled = 1;};}");
        File.WriteAllText(projectPath, project.WriteToString());
    }
    private static void AddNotificationServiceExtension(PBXProject project, string path)
    {
      #if UNITY_2017_2_OR_NEWER && !UNITY_CLOUD_BUILD
        var projectPath         = PBXProject.GetPBXProjectPath(path);
        var mainTargetGUID      = GetPBXProjectTargetGUID(project);
        var extensionTargetName = NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME;

        var exisitingPlistFile = CreateNotificationExtensionPlistFile(path);
        // If file exisits then the below has been completed before from another build
        // The below will not be updated on Append builds
        // Changes would most likely need to be made to support Append builds
        if (exisitingPlistFile)
        {
            return;
        }

        var extensionGUID = PBXProjectExtensions.AddAppExtension(
            project,
            mainTargetGUID,
            extensionTargetName,
            PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + "." + extensionTargetName,
            extensionTargetName + "/" + "Info.plist" // Unix path as it's used by Xcode
            );

        var sourceDestination = extensionTargetName + "/NotificationService";
        project.AddFileToBuild(extensionGUID, project.AddFile(sourceDestination + ".h", sourceDestination + ".h", PBXSourceTree.Source));
        project.AddFileToBuild(extensionGUID, project.AddFile(sourceDestination + ".m", sourceDestination + ".m", PBXSourceTree.Source));

        foreach (var framework in FRAMEWORKS_TO_ADD)
        {
            project.AddFrameworkToProject(extensionGUID, framework, true);
        }

        // Makes it so that the extension target is Universal (not just iPhone) and has an iOS 10 deployment target
        project.SetBuildProperty(extensionGUID, "TARGETED_DEVICE_FAMILY", "1,2");
        project.SetBuildProperty(extensionGUID, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");

        project.SetBuildProperty(extensionGUID, "ARCHS", "$(ARCHS_STANDARD)");
        project.SetBuildProperty(extensionGUID, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);

        foreach (var type in new string[] { "m", "h" })
        {
            if (!File.Exists(path + DIR_CHAR + sourceDestination + "." + type))
            {
                FileUtil.CopyFileOrDirectory(OS_PLATFORM_LOCATION + "iOS" + DIR_CHAR + "NotificationService." + type, path + DIR_CHAR + sourceDestination + "." + type);
            }
        }

        project.AddBuildProperty(extensionGUID, "LIBRARY_SEARCH_PATHS", "$(PROJECT_DIR)/Libraries/OneSignal/Platforms/iOS");
        project.WriteToFile(projectPath);

        // Add libOneSignal.a to the OneSignalNotificationServiceExtension target
        var contents = File.ReadAllText(projectPath);

        // This method only modifies the PBXProject string passed in (contents).
        // After this method finishes, we must write the contents string to disk
        InsertStaticFrameworkIntoTargetBuildPhaseFrameworks("libOneSignal", "CD84C25F20742FAB0035D524", extensionGUID, ref contents, project);
        File.WriteAllText(projectPath, contents);

        AddOrUpdateEntitlements(
            path,
            project,
            extensionGUID,
            extensionTargetName,
            new HashSet <EntitlementOptions> {
            EntitlementOptions.AppGroups
        }
            );
      #endif
    }
Beispiel #5
0
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {
        var separator = Path.DirectorySeparatorChar;

        string     projectPath = PBXProject.GetPBXProjectPath(path);
        PBXProject project     = new PBXProject();

        project.ReadFromString(File.ReadAllText(projectPath));
        string targetName = PBXProject.GetUnityTargetName();
        string targetGUID = project.TargetGuidByName(targetName);

        // UserNotifications.framework is required by libOneSignal.a
        project.AddFrameworkToProject(targetGUID, "UserNotifications.framework", false);

         #if UNITY_2017_2_OR_NEWER && !UNITY_CLOUD_BUILD
        var platformsLocation         = "Assets" + separator + "OneSignal" + separator + "Platforms" + separator;
        var extensionTargetName       = "OneSignalNotificationServiceExtension";
        var pathToNotificationService = path + separator + extensionTargetName;

        var notificationServicePlistPath = pathToNotificationService + separator + "Info.plist";

        PlistDocument notificationServicePlist = new PlistDocument();

        //if this is a rebuild, we've already added the extension service, no need to run this script a second time
        if (File.Exists(notificationServicePlistPath))
        {
            // update version
            notificationServicePlist.ReadFromFile(platformsLocation + "iOS" + separator + "Info.plist");
            notificationServicePlist.root.SetString("CFBundleShortVersionString", PlayerSettings.bundleVersion);
            notificationServicePlist.root.SetString("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString());
            notificationServicePlist.WriteToFile(notificationServicePlistPath);
            return;
        }

        Directory.CreateDirectory(pathToNotificationService);

        notificationServicePlist.ReadFromFile(platformsLocation + "iOS" + separator + "Info.plist");
        notificationServicePlist.root.SetString("CFBundleShortVersionString", PlayerSettings.bundleVersion);
        notificationServicePlist.root.SetString("CFBundleVersion", PlayerSettings.iOS.buildNumber.ToString());

        var notificationServiceTarget = PBXProjectExtensions.AddAppExtension(project, targetGUID, extensionTargetName, PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + "." + extensionTargetName, notificationServicePlistPath);

        var sourceDestination = extensionTargetName + "/NotificationService";

        project.AddFileToBuild(notificationServiceTarget, project.AddFile(sourceDestination + ".h", sourceDestination + ".h", PBXSourceTree.Source));
        project.AddFileToBuild(notificationServiceTarget, project.AddFile(sourceDestination + ".m", sourceDestination + ".m", PBXSourceTree.Source));

        var frameworks = new string[] { "NotificationCenter.framework", "UserNotifications.framework", "UIKit.framework", "SystemConfiguration.framework" };

        foreach (string framework in frameworks)
        {
            project.AddFrameworkToProject(notificationServiceTarget, framework, true);
        }

        //makes it so that the extension target is Universal (not just iPhone) and has an iOS 10 deployment target
        project.SetBuildProperty(notificationServiceTarget, "TARGETED_DEVICE_FAMILY", "1,2");
        project.SetBuildProperty(notificationServiceTarget, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");

        project.SetBuildProperty(notificationServiceTarget, "ARCHS", "$(ARCHS_STANDARD)");
        project.SetBuildProperty(notificationServiceTarget, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);

        notificationServicePlist.WriteToFile(notificationServicePlistPath);

        foreach (string type in new string[] { "m", "h" })
        {
            if (!File.Exists(path + separator + sourceDestination + "." + type))
            {
                FileUtil.CopyFileOrDirectory(platformsLocation + "iOS" + separator + "NotificationService." + type, path + separator + sourceDestination + "." + type);
            }
        }

        project.WriteToFile(projectPath);

        //add libOneSignal.a to the OneSignalNotificationServiceExtension target
        string contents = File.ReadAllText(projectPath);

        //this method only modifies the PBXProject string passed in (contents).
        //after this method finishes, we must write the contents string to disk
        InsertStaticFrameworkIntoTargetBuildPhaseFrameworks("libOneSignal", "CD84C25F20742FAB0035D524", notificationServiceTarget, ref contents, project);
        File.WriteAllText(projectPath, contents);
         #else
        project.WriteToFile(projectPath);

        string contents = File.ReadAllText(projectPath);
         #endif

        // enable the Notifications capability in the main app target
        project.ReadFromString(contents);
        var entitlementPath = path + separator + targetName + separator + targetName + ".entitlements";

        PlistDocument entitlements = new PlistDocument();

        if (File.Exists(entitlementPath))
        {
            entitlements.ReadFromFile(entitlementPath);
        }

         #if !UNITY_CLOUD_BUILD && ADD_APP_GROUP
        if (entitlements.root["aps-environment"] == null)
        {
            entitlements.root.SetString("aps-environment", "development");
        }

        if (entitlements.root["com.apple.security.application-groups"] == null)
        {
            var groups = entitlements.root.CreateArray("com.apple.security.application-groups");
            groups.AddString("group." + PlayerSettings.applicationIdentifier + ".onesignal");
        }
         #endif

        entitlements.WriteToFile(entitlementPath);

        // Copy the entitlement file to the xcode project
        var entitlementFileName = Path.GetFileName(entitlementPath);
        var unityTarget         = PBXProject.GetUnityTargetName();
        var relativeDestination = unityTarget + "/" + entitlementFileName;

        // Add the pbx configs to include the entitlements files on the project
        project.AddFile(relativeDestination, entitlementFileName);
        project.AddBuildProperty(targetGUID, "CODE_SIGN_ENTITLEMENTS", relativeDestination);

        // Add push notifications as a capability on the target
        project.AddBuildProperty(targetGUID, "SystemCapabilities", "{com.apple.Push = {enabled = 1;};}");
        File.WriteAllText(projectPath, project.WriteToString());
    }
Beispiel #6
0
    private static void AddNotificationServiceExtension(PBXProject project, string path)
    {
#if !UNITY_CLOUD_BUILD
        var projectPath         = PBXProject.GetPBXProjectPath(path);
        var mainTargetGUID      = GetPBXProjectTargetGUID(project);
        var extensionTargetName = NotificationServiceExtensionTargetName;

        var exisitingPlistFile = CreateNotificationExtensionPlistFile(path);
        // If file exisits then the below has been completed before from another build
        // The below will not be updated on Append builds
        // Changes would most likely need to be made to support Append builds
        if (exisitingPlistFile)
        {
            return;
        }

        var extensionGUID = PBXProjectExtensions.AddAppExtension(
            project,
            mainTargetGUID,
            extensionTargetName,
            PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS) + "." + extensionTargetName,
            extensionTargetName + "/" + "Info.plist" // Unix path as it's used by Xcode
            );

        AddNotificationServiceSourceFilesToTarget(project, extensionGUID, path);

        foreach (var framework in FrameworksToAdd)
        {
            project.AddFrameworkToProject(extensionGUID, framework, true);
        }

        // Makes it so that the extension target is Universal (not just iPhone) and has an iOS 10 deployment target
        project.SetBuildProperty(extensionGUID, "TARGETED_DEVICE_FAMILY", "1,2");
        project.SetBuildProperty(extensionGUID, "IPHONEOS_DEPLOYMENT_TARGET", "10.0");
        project.SetBuildProperty(extensionGUID, "ARCHS", "$(ARCHS_STANDARD)");
        project.SetBuildProperty(extensionGUID, "DEVELOPMENT_TEAM", PlayerSettings.iOS.appleDeveloperTeamID);

        project.AddBuildProperty(extensionGUID, "LIBRARY_SEARCH_PATHS",
                                 $"$(PROJECT_DIR)/Libraries/{PluginLibrariesPath.Replace("\\", "/")}");
        project.WriteToFile(projectPath);

        // Add libOneSignal.a to the OneSignalNotificationServiceExtension target
        var contents = File.ReadAllText(projectPath);

        // This method only modifies the PBXProject string passed in (contents).
        // After this method finishes, we must write the contents string to disk
        InsertStaticFrameworkIntoTargetBuildPhaseFrameworks("libOneSignal", "CD84C25F20742FAB0035D524",
                                                            extensionGUID,
                                                            ref contents, project);
        File.WriteAllText(projectPath, contents);

        AddOrUpdateEntitlements(
            path,
            project,
            extensionGUID,
            extensionTargetName,
            new HashSet <EntitlementOptions> {
            EntitlementOptions.AppGroups
        }
            );
#endif
    }