83
votes

I am trying to configure my app to support push notification, but keep getting this error: Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application" UserInfo=0x1f818fc0 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application}

  • I did enable my app to support push notifications (only for development at this point).
  • I did create the provisioning profile AFTER enabling the push notifications.
  • My bundle identifier is the same as appears in the profile.
  • In my profile file there is a string: aps-environment
  • I made sure that in both project and target under the "Code signing identity" section ALL the values are selected to be my provisioning profile.

Any more ideas? Thanks.

11
No insight about this one? It looks like something many would encounter...bashan

11 Answers

64
votes

In my case, the stuff in my provisioning profile:

security cms -D -i ~/Downloads/spolskyDevelop.mobileprovision 
...
<key>application-identifier</key>
<string>P5GM95Q9VV.com.dca.spolsky</string>
<key>aps-environment</key>
<string>development</string>

Was different than the stuff in the app that was actually built (you can find out where it is built by looking at Xcode's Logs tab)

codesign -d --entitlements - '/Users/drew/Library/Developer/Xcode/DerivedData/spolsky-bdbtdfjeeywhqzccpmmceqvnhgtm/Build/Products/Debug-iphoneos/spolsky-ios.app'
<dict>
    <key>application-identifier</key>
    <string>Y2X6Z7Z2GR.com.dca.spolsky-ios</string>
    <key>get-task-allow</key>
    <true/>
    <key>keychain-access-groups</key>
    <array>
        <string>Y2X6Z7Z2GR.com.dca.spolsky-ios</string>
    </array>
</dict>

This was true even though the "Currently Matches" hint text under Code Signing Identity was indicating the correct provisioning profile--weird, huh? To make a weird story weirder, the correct provisioning profile was being installed on the device when I ran, (Settings->General->Profiles) leading me to believe the provisioning profile was right--but it was falling back to a wildcard ID when the app was actually launched.

The clue was the difference in the output of these two commands:

Y2X6Z7Z2GR.com.dca.spolsky-ios vs P5GM95Q9VV.com.dca.spolsky

When I made the bolded part match, the italicized part changed to match automatically. In addition, the output of security and codesign were in agreement, and no more aps-entitlement error.

My guess here is that XCode was using a wildcard-style match on my non-wildcard ID. ("spolsky" is, after all, quite nearly "spolsky-ios"), and this explains the "Currently Matches" output. But something in the build chain is more strict about this, so it falls back to an actual wildcard ID during the build.

37
votes

The problem is due to current Provisioning Profile does not contain APNS information, as the Provisioning Profile is created BEFORE creating the APNS certificate.

Therefore, to solve the problem, create a new Provisioning Profile & select the Provisioning Profile in Xcode will clear the error.

28
votes

I had this same problem after push notifications were already working on my device. Seemed like Xcode cached something incorrectly so refreshing it fixed my problem:

  1. Xcode -> Preferences -> Accounts
  2. View Details for your Developer Account
  3. Just click Refresh for the Provision Profiles a few times. I was seeing these change with each refresh. Keep refreshing until they stop changing.
9
votes

i got this error and did not create a new provisioning profile; created a new one and then had to restart xCode but works now!

9
votes

Be aware that the bundle identifier must match the provisioning profile in a case-sensitive manner. I had something like com.FirstLast.appname when it should have been com.firstlast.appname. Well, that kept me busy for quite a while.

9
votes

I fixed this problem by enabling push notification capability.

Select Project > Target > Capabilities > Turn On Push notifications

check out the image

3
votes

If this problem happened suddenly after upgrading to Xcode 8 you might find a warning in the capabilities screen that'll allow you to fix this automatically:

Select Project > Target > Capabilities > Turn On Push notifications

2
votes

Product->Clean did it for me. Hopefully it does it for someone else.

2
votes

I ran into this in what's possibly a non-frequent use case. I'm working with Xcode 6 and iOS 8. I was creating a series of entirely new app projects, and attempting to re-use the same bundle ID, so as not to need to go through all of the portal-side setup again. This too was just for sandbox testing.

I would find that, while the setup and registration would work fine for the first app, if I deleted that app from the device, set the same bundle ID for the second app, and then attempted to register PNs with that app, I would get this error.

My solution was to:

  1. Remove the associated provisioning profile from the device.
  2. Remove the associated provisioning profile from Xcode.
  3. Close Xcode
  4. Double-click my downloaded .mobileprovision file to install it back into Xcode
  5. Open the project and deploy it.

This approach seems to jar loose whatever previous state was there, and I'm able to register for PNs with the new app / same bundle ID.

2
votes

For those who got this error in ios 10, you can try following steps:

1.clean your project.
2.and check Targets -> Capabilities -> Push Notification.If it is off,On it.

And problem will solve.The above steps help me to get rid off this problem. Hope it will helpful to someone.

0
votes

New provisioning profile worked for me. Make sure to use a Developer profile if you are in development (don't use Ad Hoc). It was fine after that, no idea why the old one stopped working right.