Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I enable Remote Push Notification Capability using Unity Cloud Build?

Discussion in 'iOS and tvOS' started by DrakenDev, Feb 23, 2017.

  1. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
    Hi, I've been struggling on and off for a few weeks now to get Push Notifications working for my Unity Game.
    I always get the following message when trying to submit my IPA file.
    I think the issues is I need to enable Capabilities>Background Modes>Remote Notifications.

    And I've tried doing this by adding the following into the info.plist. Which appears to not do anything
    Code (csharp):
    1.                 <key>UIBackgroundModes</key>
    2.                 <array>
    3.                     <string>remote-notification</string>
    4.                 </array>
    The thing is, I'm using Unity Cloud Build for everything and have no-idea how this is meant to work for that. I've done extensive Googling and cannot come up with ANYTHING. It's really surprising me that in my search no-one has come up against this issue... I really need help...

    Thanks in advance!
     
    Last edited: Feb 23, 2017
    tonybf and AMiN-Unity like this.
  2. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
  3. lindsaytalbot

    lindsaytalbot

    Joined:
    Sep 11, 2015
    Posts:
    28
    Having similar problem but with iCloud. Suspect whatever solution you get will also help with mine
     
  4. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Got push notifications to work in UCB via this post might help with other capabilities
     
  5. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
    YEP! THAT WORKED!
    I skipped the part about XCode and creating an entitlements file. I just put the contents of my "AppName.Entitlements" file as
    Code (csharp):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4.   <dict>
    5.     <key>aps-environment</key>
    6.     <string>production</string>
    7.   </dict>
    8. </plist>
    Pointed the .UnityPackage at the file, commited to Unity Cloud Build and after uploading to Apple, voila! It worked!
     
    migwellian and a_p_u_r_o like this.
  6. ashwin_dinesh

    ashwin_dinesh

    Joined:
    Feb 21, 2019
    Posts:
    3
    This is how you can add background-modes through post-process-build script:

    Code (CSharp):
    1.  
    2. PBXProject proj = new PBXProject();
    3. string projPath = PBXProject.GetPBXProjectPath(buildPath);
    4. proj.ReadFromFile(projPath);
    5.  
    6. string targetName = PBXProject.GetUnityTargetName();
    7. string mainTarget = proj.TargetGuidByName(targetName);
    8.  
    9. // Enable background mode
    10. proj.AddCapability(mainTarget, PBXCapabilityType.BackgroundModes);
    11.  
    12. proj.WriteToFile(projPath);
    13. File.WriteAllText(projPath, proj.WriteToString());
    14.  
    15. // Add background modes
    16. string plistPath = buildPath + "/Info.plist";
    17. PlistDocument plist = new PlistDocument();
    18. plist.ReadFromString(File.ReadAllText(plistPath));
    19. PlistElementDict rootDict = plist.root;
    20. PlistElementArray arr = rootDict.CreateArray("UIBackgroundModes");
    21. arr.AddString("remote-notification");
    22. arr.AddString("fetch");
    23. File.WriteAllText(plistPath, plist.WriteToString());
    24.  
     
    khaled24 and AvenousLee117 like this.
  7. studentutu

    studentutu

    Joined:
    Oct 22, 2017
    Posts:
    105
    Just don't forget :
    Push notification entitile value is choosen from Xcode - when in simple test build it will give you either development or production value!
    Also, don't forget to check if the frameworks are linked! (Xcode API is old so it does'nt know about new Xcode!)
     
  8. David_132

    David_132

    Joined:
    Dec 14, 2018
    Posts:
    6
    We are using a third party Scalefusion iOS MDM for remote push notification.
     
  9. resilio

    resilio

    Joined:
    Sep 7, 2017
    Posts:
    12
    If anyone already uses `ProjectCapabilityManager`, one can simply add these lines:

    Code (CSharp):
    1.  
    2. var capManager = new ProjectCapabilityManager(projPath, entitlementsPath, "Unity-iPhone");
    3. capManager.AddBackgroundModes(BackgroundModesOptions.BackgroundFetch);    capManager.AddBackgroundModes(BackgroundModesOptions.RemoteNotifications);
    4. capManager.WriteToFile();
    5.  
     
    munkiki7 and tonybf like this.
  10. adrian_bp

    adrian_bp

    Joined:
    Nov 25, 2019
    Posts:
    1
    Where did you get the entitlementsPath? did you need to create an empty file?
     
  11. k_belonogov

    k_belonogov

    Joined:
    Jan 12, 2016
    Posts:
    10
    var entitlementsFileName = pbxProject.GetBuildPropertyForAnyConfig(mainTarget, "CODE_SIGN_ENTITLEMENTS") ?? Application.identifier + ".entitlements";

    var capManager = new ProjectCapabilityManager(projPath, entitlementsFileName, "Unity-iPhone");
     
    Last edited: Aug 25, 2020
    munkiki7 likes this.
  12. cmersereau

    cmersereau

    Joined:
    Nov 6, 2020
    Posts:
    52
    Time for another update to this thread, with a few pointers that might help people that are in the same situation I was.

    The previous comments on this thread are very good, but deprecated methods and specific circumstances weren't allowing them to get the job done that I needed.

    Deprecated methods
    You'll see in my solution that there is a new way of getting the GUID that you'll need. I actually could not find a way of getting the target name by method ("Unity-iPhone" by default I believe) as the old one is deprecated and throws an error if you try to use it. My project is using the default, so unless you have explicitly changed yours somehow then this should work just fine.

    Plugin also accessing entitlements
    Even after fixing all errors and doing my best to get the naming conventions correct, I still wasn't seeing the right entitlements added. Turns out one of the SDK's I added to my project was also accessing the entitlements file, and for some reason their method was setting the name of my entitlements file (sorry, didn't find out why this is or if I could stop it). Fortunately I was able to edit their scripts and explicitly set the name of the entitlements file in both theirs and my scripts.

    Good knowledge to have
    For your enrichment, it seems there might be two ways of fixing this error. Some here appear to be fixing it by adding remote notifications. At one point, when I tried adding remote notifications I got an error that it was already there. Yet I was still getting the same error as the OP. What I needed was to have the Push Notifications capability added. To do this, you need to have the "aps-environment" key added to the entitlements file, with either the "production" or "development" value. This is what is happening in the capManager.AddPushNotifications() method (True for development. Haven't yet learned the full implications of true or false yet).

    For newbies
    If you're really new to this concept, it might be helpful to know that any method with the
    [PostProcessBuild]
    attribute will run after the build, so you can put this function anywhere you like.

    Code (CSharp):
    1. [PostProcessBuild]
    2.     public static void AddToEntitlements(BuildTarget buildTarget, string buildPath)
    3.     {
    4.  
    5.         if (buildTarget != BuildTarget.iOS) return;
    6.  
    7.         // get project info
    8.         string pbxPath = PBXProject.GetPBXProjectPath(buildPath);
    9.         var proj = new PBXProject();
    10.         proj.ReadFromFile(pbxPath);
    11.         var guid = proj.GetUnityMainTargetGuid();
    12.  
    13.         // get entitlements path
    14.         string[] idArray = Application.identifier.Split('.');
    15.         var entitlementsPath = $"Unity-iPhone/{idArray[idArray.Length - 1]}.entitlements";
    16.  
    17.         // create capabilities manager
    18.         var capManager = new ProjectCapabilityManager(pbxPath, entitlementsPath, null, guid);
    19.  
    20.         // Add necessary capabilities
    21.         capManager.AddPushNotifications(true);
    22.  
    23.         // Write to file
    24.         capManager.WriteToFile();
    25.     }
     
    Last edited: Mar 16, 2022
    migwellian, cdr9042, raraco and 6 others like this.
  13. francismoy

    francismoy

    Joined:
    Jul 5, 2017
    Posts:
    46
    Not sure if this is going to work on UCB, since I haven't tested on this platform, but I recently wanted to add the Push Notifications capability automatically (so I didn't need to edit anything in Xcode) and I had many problems. I'm just going to show the code that finally worked for me, which doesn't require using the ProjectCapabilityManager.

    Just change {your-game-name} with the name of your game as it appears on Player Settings. Also, this will only work if you don't have any other entitlements file, since I hardcode the entitlements file contents in a string. I'm using Unity 2019.4.28 and Xcode 12.5:

    Code (CSharp):
    1. [PostProcessBuild()]
    2. public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    3. {      
    4.         if (target != BuildTarget.iOS)
    5.             return;
    6.  
    7.         string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
    8.         PBXProject proj = new PBXProject();
    9.         proj.ReadFromString(File.ReadAllText(projPath));
    10.  
    11.         MyLogger.Debug("Adding Push Notifications capability");
    12.         AddPushNotificationCapability(pathToBuiltProject, proj);
    13.  
    14.         proj.WriteToFile(projPath);
    15. }
    16.  
    17. private static void AddPushNotificationCapability(string buildPath, PBXProject proj)
    18. {
    19.         string entitlementsPath = AddEntitlementsFile(buildPath, proj);
    20.         proj.AddCapability(proj.GetUnityMainTargetGuid(), PBXCapabilityType.PushNotifications, entitlementsPath);  
    21. }
    22.  
    23. private static string AddEntitlementsFile(string buildPath, PBXProject proj)
    24. {
    25.         const string contents = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    26.  <!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
    27.   <plist version = ""1.0"">
    28.    <dict>
    29.        <key>aps-environment</key>
    30.        <string>development</string>
    31.    </dict>
    32.    </plist>";
    33.  
    34.         string entitlementsPath = $"{buildPath}/Unity-iPhone/{your-game-name}.entitlements";
    35.  
    36.         File.WriteAllText(entitlementsPath, contents);
    37.         return entitlementsPath;
    38. }