Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by pcpaukm · Aug 02, 2016 at 11:57 AM · iosxcodedefaultpostprocesspush notification

Enable Push Notification in XCode project by default?

When Unity project compiled, the generated XCode project doesn't have Push Notification on. What is the way to make it on by default after compile? Do I have to create a postprocessing like the below? If so, how?

http://answers.unity3d.com/questions/1066927/postprocessing-ios-activate-background-mode-for-pu.html

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by CelitoOokaIsland · Jun 12, 2017 at 04:36 PM

I manage to get it working by a C# script, but since this build setting need an .entitlement file in your xcode project, first you will need to create a text file called [projectName].entitlement anywhere in your project with this content:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
   <key>aps-environment</key>
   <string>development</string>
 </dict>
 </plist>

Now you can add this script on the Editor folder:

 using System.IO;
 using JetBrains.Annotations;
 using UnityEditor;
 using UnityEditor.Callbacks;
 using UnityEditor.iOS.Xcode;
 using UnityEngine;

 public class PostProcessBuildScript : ScriptableObject
 {
     public DefaultAsset entitlementsFile;

     [PostProcessBuild, UsedImplicitly]
     public static void ChangesToXcode(BuildTarget buildTarget, string pathToBuiltProject)
     {
         // Get project PBX file
         var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
         var proj = new PBXProject();
         proj.ReadFromString(File.ReadAllText(projPath));
         var target = proj.TargetGuidByName("Unity-iPhone");

         // Create an instance of this ScriptableObject so we can point to its entitlements file
         var dummy = CreateInstance<PostProcessBuildScript>();
         var entitlementsFile = dummy.entitlementsFile;
         DestroyImmediate(dummy);
         if (entitlementsFile != null)
         {
             // Copy the entitlement file to the xcode project
             var entitlementPath = AssetDatabase.GetAssetPath(entitlementsFile);
             var entitlementFileName = Path.GetFileName(entitlementPath);
             var unityTarget = PBXProject.GetUnityTargetName();
             var relativeDestination = unityTarget + "/" + entitlementFileName;
             FileUtil.CopyFileOrDirectory(entitlementPath, pathToBuiltProject + "/" + relativeDestination);

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

             // Add push notifications as a capability on the target
             proj.AddBuildProperty(target, "SystemCapabilities", "{com.apple.Push = {enabled = 1;};}");
         }

         // Save the changed configs to the file
         File.WriteAllText(projPath, proj.WriteToString());
     }
 }

After that you just define the entitlementsFile variable for the script in the inspector window and you should now have the push notification activated by default when you build for iOS.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TigerFish89 · Sep 10, 2017 at 08:28 AM 0
Share

Thank you. Your work helpul to me. But I have a another errors. When I build finish. I got a error message in xcode. here is the error code.

The file "/Users/Test/jenkins/Agent/workspace/Project/build/Unity-iPhone/normal.entitlements" could not be opened. Verify the value of the CODE_SIGN_ENTITLE$$anonymous$$ENTS build setting for target "Unity-iPhone" and build configuration "Release" is correct and that the file exists on disk.

What am I miss?

avatar image LimeDrops TigerFish89 · Feb 11, 2020 at 06:33 AM 0
Share

The same error occurred when the project was first created. However, when recreated (overwritten) in the same path, it worked.

2018.4.14

avatar image
2

Answer by Sarazan · Dec 15, 2018 at 08:45 PM

Just wanted to further develop @CelitoOokaIsland's solution. I removed the need for a separate entitlements asset, and just embedded the string into the build script.

In theory, you should be able to drop this script into any project and have it work:

 using UnityEditor;
 using UnityEditor.Callbacks;
 using UnityEngine;
 using System.IO;
 using UnityEditor.iOS.Xcode;
 
 public class EntitlementsPostProcess
 {
     private const string entitlements = @"
     <?xml version=""1.0"" encoding=""UTF-8\""?>
     <!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
     <plist version=""1.0"">
         <dict>
             <key>aps-environment</key>
             <string>development</string>
         </dict>
     </plist>";
     
     [PostProcessBuild]
     public static void OnPostProcess(BuildTarget buildTarget, string buildPath)
     {
         if (buildTarget != BuildTarget.iOS) return;
 
         var file_name = "unity.entitlements";
         var proj_path = PBXProject.GetPBXProjectPath(buildPath);
         var proj = new PBXProject();
         proj.ReadFromFile(proj_path);
 
         // target_name = "Unity-iPhone"
         var target_name = PBXProject.GetUnityTargetName();
         var target_guid = proj.TargetGuidByName(target_name);        
         var dst = buildPath + "/" + target_name + "/" + file_name;
         try
         {
             File.WriteAllText(dst, entitlements);
             proj.AddFile(target_name + "/" + file_name, file_name);
             proj.AddBuildProperty(target_guid, "CODE_SIGN_ENTITLEMENTS", target_name + "/" + file_name);
             proj.WriteToFile(proj_path);
         }
         catch (IOException e)
         {
             Debug.LogWarning($"Could not copy entitlements. Probably already exists. ({e})");
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PostProcessing iOS Activate Background Mode for Push Notifications 1 Answer

How can I set a push notification of Capability in PostProcess? 0 Answers

Not able to add iCloud to XCode project via PostProcessing script using the XCodeAPI 1 Answer

Which version of Xcode does Unity 3.5.5f3 support? 1 Answer

iOS - Mimic Native Layout 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges