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
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…
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!)
If anyone already uses ProjectCapabilityManager, one can simply add these lines:
var capManager = new ProjectCapabilityManager(projPath, entitlementsPath, "Unity-iPhone");
capManager.AddBackgroundModes(BackgroundModesOptions.BackgroundFetch); capManager.AddBackgroundModes(BackgroundModesOptions.RemoteNotifications);
capManager.WriteToFile();
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.
[PostProcessBuild]
public static void AddToEntitlements(BuildTarget buildTarget, string buildPath)
{
if (buildTarget != BuildTarget.iOS) return;
// get project info
string pbxPath = PBXProject.GetPBXProjectPath(buildPath);
var proj = new PBXProject();
proj.ReadFromFile(pbxPath);
var guid = proj.GetUnityMainTargetGuid();
// get entitlements path
string[] idArray = Application.identifier.Split('.');
var entitlementsPath = $"Unity-iPhone/{idArray[idArray.Length - 1]}.entitlements";
// create capabilities manager
var capManager = new ProjectCapabilityManager(pbxPath, entitlementsPath, null, guid);
// Add necessary capabilities
capManager.AddPushNotifications(true);
// Write to file
capManager.WriteToFile();
}
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: