Guest User

Untitled

a guest
Sep 14th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.09 KB | None | 0 0
  1. #if UNITY_IOS
  2. using System;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEditor.Callbacks;
  7. using UnityEditor.iOS.Xcode;
  8. using UnityEngine;
  9. using Unity.Notifications;
  10. using Unity.Notifications.iOS;
  11.  
  12. public class iOSNotificationPostProcessor : MonoBehaviour
  13. {
  14.     [PostProcessBuild]
  15.     public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
  16.     {
  17.         if (buildTarget != BuildTarget.iOS)
  18.             return;
  19.  
  20.         // Check if we have the minimal iOS version set.
  21.         bool hasMinOSVersion;
  22.         try
  23.         {
  24.             var requiredVersion = new Version(10, 0);
  25.             var currentVersion = new Version(PlayerSettings.iOS.targetOSVersionString);
  26.             hasMinOSVersion = currentVersion >= requiredVersion;
  27.         }
  28.         catch (Exception)
  29.         {
  30.             hasMinOSVersion = false;
  31.         }
  32.  
  33.         if (!hasMinOSVersion)
  34.             Debug.Log("UserNotifications framework is only available on iOS 10.0+, please make sure that you set a correct `Target minimum iOS Version` in Player Settings.");
  35.  
  36.         var settings = NotificationSettingsManager.Initialize().iOSNotificationSettingsFlat;
  37.  
  38.         var needLocationFramework = (bool)settings.Find(i => i.Key == "UnityUseLocationNotificationTrigger").Value;
  39.         var addPushNotificationCapability = (bool)settings.Find(i => i.Key == "UnityAddRemoteNotificationCapability").Value;
  40.  
  41.         var useReleaseAPSEnv = false;
  42.         if (addPushNotificationCapability)
  43.         {
  44.             var useReleaseAPSEnvSetting = settings.Find(i => i.Key == "UnityUseAPSReleaseEnvironment");
  45.             if (useReleaseAPSEnvSetting != null)
  46.                 useReleaseAPSEnv = (bool)useReleaseAPSEnvSetting.Value;
  47.         }
  48.  
  49.         PatchPBXProject(path, needLocationFramework, addPushNotificationCapability, useReleaseAPSEnv);
  50.         PatchPlist(path, settings, addPushNotificationCapability);
  51.         PatchPreprocessor(path, needLocationFramework, addPushNotificationCapability);
  52.     }
  53.  
  54.     private static void PatchPBXProject(string path, bool needLocationFramework, bool addPushNotificationCapability, bool useReleaseAPSEnv)
  55.     {
  56.         var pbxProjectPath = PBXProject.GetPBXProjectPath(path);
  57.  
  58.         var needsToWriteChanges = false;
  59.  
  60.         var pbxProject = new PBXProject();
  61.         pbxProject.ReadFromString(File.ReadAllText(pbxProjectPath));
  62.  
  63.         string mainTarget;
  64.         string unityFrameworkTarget;
  65.  
  66.         var unityMainTargetGuidMethod = pbxProject.GetType().GetMethod("GetUnityMainTargetGuid");
  67.         var unityFrameworkTargetGuidMethod = pbxProject.GetType().GetMethod("GetUnityFrameworkTargetGuid");
  68.  
  69.         if (unityMainTargetGuidMethod != null && unityFrameworkTargetGuidMethod != null)
  70.         {
  71.             mainTarget = (string)unityMainTargetGuidMethod.Invoke(pbxProject, null);
  72.             unityFrameworkTarget = (string)unityFrameworkTargetGuidMethod.Invoke(pbxProject, null);
  73.         }
  74.         else
  75.         {
  76.             mainTarget = pbxProject.TargetGuidByName("Unity-iPhone");
  77.             unityFrameworkTarget = mainTarget;
  78.         }
  79.  
  80.         // Add necessary frameworks.
  81.         if (!pbxProject.ContainsFramework(unityFrameworkTarget, "UserNotifications.framework"))
  82.         {
  83.             pbxProject.AddFrameworkToProject(unityFrameworkTarget, "UserNotifications.framework", true);
  84.             needsToWriteChanges = true;
  85.         }
  86.         if (needLocationFramework && !pbxProject.ContainsFramework(unityFrameworkTarget, "CoreLocation.framework"))
  87.         {
  88.             pbxProject.AddFrameworkToProject(unityFrameworkTarget, "CoreLocation.framework", false);
  89.             needsToWriteChanges = true;
  90.         }
  91.  
  92.         if (needsToWriteChanges)
  93.             File.WriteAllText(pbxProjectPath, pbxProject.WriteToString());
  94.  
  95.         // Update the entitlements file.
  96.         if (addPushNotificationCapability)
  97.         {
  98.             var entitlementsFileName = pbxProject.GetBuildPropertyForAnyConfig(mainTarget, "CODE_SIGN_ENTITLEMENTS");
  99.             if (entitlementsFileName == null)
  100.             {
  101.                 var bundleIdentifier = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
  102.                 entitlementsFileName = string.Format("{0}.entitlements", bundleIdentifier.Substring(bundleIdentifier.LastIndexOf(".") + 1));
  103.             }
  104.  
  105.             var capManager = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, "Unity-iPhone");
  106.             capManager.AddPushNotifications(!useReleaseAPSEnv);
  107.             capManager.WriteToFile();
  108.         }
  109.     }
  110.  
  111.     private static void PatchPlist(string path, List<Unity.Notifications.NotificationSetting> settings, bool addPushNotificationCapability)
  112.     {
  113.         var plistPath = path + "/Info.plist";
  114.         var plist = new PlistDocument();
  115.         plist.ReadFromString(File.ReadAllText(plistPath));
  116.  
  117.         var rootDict = plist.root;
  118.         var needsToWriteChanges = false;
  119.  
  120.         // Add all the settings to the plist.
  121.         foreach (var setting in settings)
  122.         {
  123.             if (ShouldAddSettingToPlist(setting, rootDict))
  124.             {
  125.                 needsToWriteChanges = true;
  126.                 if (setting.Value.GetType() == typeof(bool))
  127.                 {
  128.                     rootDict.SetBoolean(setting.Key, (bool)setting.Value);
  129.                 }
  130.                 else if (setting.Value.GetType() == typeof(PresentationOption) ||
  131.                          setting.Value.GetType() == typeof(AuthorizationOption))
  132.                 {
  133.                     rootDict.SetInteger(setting.Key, (int)setting.Value);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         // Add "remote-notification" to the list of supported UIBackgroundModes.
  139.         if (addPushNotificationCapability)
  140.         {
  141.             PlistElementArray currentBackgroundModes = (PlistElementArray)rootDict["UIBackgroundModes"];
  142.             if (currentBackgroundModes == null)
  143.                 currentBackgroundModes = rootDict.CreateArray("UIBackgroundModes");
  144.  
  145.             var remoteNotificationElement = new PlistElementString("remote-notification");
  146.             if (!currentBackgroundModes.values.Contains(remoteNotificationElement))
  147.             {
  148.                 currentBackgroundModes.values.Add(remoteNotificationElement);
  149.                 needsToWriteChanges = true;
  150.             }
  151.         }
  152.  
  153.         if (needsToWriteChanges)
  154.             File.WriteAllText(plistPath, plist.WriteToString());
  155.     }
  156.  
  157.     // If the plist doesn't contain the key, or it's value is different, we should add/overwrite it.
  158.     private static bool ShouldAddSettingToPlist(Unity.Notifications.NotificationSetting setting,
  159.         PlistElementDict rootDict)
  160.     {
  161.         if (!rootDict.values.ContainsKey(setting.Key))
  162.             return true;
  163.         else if (setting.Value.GetType() == typeof(bool))
  164.             return !rootDict.values[setting.Key].AsBoolean().Equals((bool)setting.Value);
  165.         else if (setting.Value.GetType() == typeof(PresentationOption) || setting.Value.GetType() == typeof(AuthorizationOption))
  166.             return !rootDict.values[setting.Key].AsInteger().Equals((int)setting.Value);
  167.         else
  168.             return false;
  169.     }
  170.  
  171.     private static void PatchPreprocessor(string path, bool needLocationFramework, bool addPushNotificationCapability)
  172.     {
  173.         var preprocessorPath = path + "/Classes/Preprocessor.h";
  174.         var preprocessor = File.ReadAllText(preprocessorPath);
  175.         var needsToWriteChanges = false;
  176.  
  177.         if (needLocationFramework && preprocessor.Contains("UNITY_USES_LOCATION"))
  178.         {
  179.             preprocessor = preprocessor.Replace("UNITY_USES_LOCATION 0", "UNITY_USES_LOCATION 1");
  180.             needsToWriteChanges = true;
  181.         }
  182.  
  183.         if (addPushNotificationCapability && preprocessor.Contains("UNITY_USES_REMOTE_NOTIFICATIONS"))
  184.         {
  185.             preprocessor =
  186.                 preprocessor.Replace("UNITY_USES_REMOTE_NOTIFICATIONS 0", "UNITY_USES_REMOTE_NOTIFICATIONS 1");
  187.             needsToWriteChanges = true;
  188.         }
  189.  
  190.         if (needsToWriteChanges)
  191.             File.WriteAllText(preprocessorPath, preprocessor);
  192.     }
  193. }
  194. #endif
Advertisement
Add Comment
Please, Sign In to add comment