// Majority of this code was thanks to the com.unity.mobile.notifications package
        private static void EnableIOSPushNotifications(IosSettings settings, string buildPath)
        {
            #if UNITY_IOS
            if (settings.iosPushNotificationType == IOSPushNotificationType.None)
            {
                return;
            }

            // Turning on push notifications (release/development)
            var projectPath = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
            var project     = new UnityEditor.iOS.Xcode.PBXProject();
            project.ReadFromString(System.IO.File.ReadAllText(projectPath));

            // Push Notification Capability
            var manager = new UnityEditor.iOS.Xcode.ProjectCapabilityManager(
                projectPath,
                "Entitlements.entitlements",
                targetGuid: project.GetUnityMainTargetGuid()
                );
            manager.AddPushNotifications(settings.iosPushNotificationType == IOSPushNotificationType.Development);
            manager.WriteToFile();

            // Making sure Uses Remote Notifications is on
            var preprocessorPath = buildPath + "/Classes/Preprocessor.h";
            var preprocessor     = System.IO.File.ReadAllText(preprocessorPath);
            preprocessor = preprocessor.Replace("UNITY_USES_REMOTE_NOTIFICATIONS 0", "UNITY_USES_REMOTE_NOTIFICATIONS 1");

            System.IO.File.WriteAllText(preprocessorPath, preprocessor);
            #endif
        }
Beispiel #2
0
        // Majority of this code was thanks to the com.unity.mobile.notifications package
        private void EnableIOSPushNotifications(IosSettings settings, string buildPath)
        {
            #if UNITY_IOS
            if (settings.iosPushNotificationType == IOSPushNotificationType.None)
            {
                return;
            }

            // Turning on push notifications (release/development)
            var projectPath = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
            var project     = new UnityEditor.iOS.Xcode.PBXProject();
            project.ReadFromString(System.IO.File.ReadAllText(projectPath));

            var target = project.TargetGuidByName("Unity-iPhone");
            var entitlementsFileName = project.GetBuildPropertyForConfig(target, "CODE_SIGN_ENTITLEMENTS");

            if (entitlementsFileName == null)
            {
                var bundleIdentifier = UnityEditor.PlayerSettings.GetApplicationIdentifier(UnityEditor.BuildTargetGroup.iOS);
                entitlementsFileName = string.Format("{0}.entitlements", bundleIdentifier.Substring(bundleIdentifier.LastIndexOf(".") + 1));
            }

            var pbxPath    = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath(buildPath);
            var capManager = new UnityEditor.iOS.Xcode.ProjectCapabilityManager(pbxPath, entitlementsFileName, "Unity-iPhone");
            capManager.AddPushNotifications(settings.iosPushNotificationType == IOSPushNotificationType.Development);
            capManager.WriteToFile();

            // Making sure Uses Remote Notifications is on
            var preprocessorPath = buildPath + "/Classes/Preprocessor.h";
            var preprocessor     = System.IO.File.ReadAllText(preprocessorPath);
            preprocessor = preprocessor.Replace("UNITY_USES_REMOTE_NOTIFICATIONS 0", "UNITY_USES_REMOTE_NOTIFICATIONS 1");

            System.IO.File.WriteAllText(preprocessorPath, preprocessor);
            #endif
        }