84

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.

bashan
  • 3,572
  • 6
  • 41
  • 58

11 Answers11

64

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.

Drew
  • 8,675
  • 6
  • 43
  • 41
  • 7
    I experienced a similar issue: the Project 'Build Settings' were different than the TARGET 'Build Settings' ... in my case, my Target settings were defaulting to the wrong provisioning profiles. – Rob Mar 13 '13 at 17:01
  • 1
    Props on discovering this, I encountered the same issue. – Codemonk May 19 '13 at 04:04
  • 1
    One of my coworkers had this provisioning issue. However, I was only able to select the non-wildcard provisioning key after going into the profile settings in Xcode and refreshing the list of certificates/provisioning keys. – cbh2000 Nov 26 '13 at 21:45
  • 1
    My target settings for 'debug' were using iOS Developer/Automatic which was causing my .app to come out with the wrong Code Signing Identity. I had to explicitly set my project & target's settings for both debug & release to use my new Code Signing ID, my problem was fixed. It's not just your provisioning profile, gotta check those Code Signing Identities too. Couldn't have solved it without these awesome terminal commands! – self.name Jun 05 '14 at 18:31
  • 6
    Just for the record: for push notifications to work, one has to use a **specific** Provisioning Profile, with the app's bundle ID, and **not** one with the __*__ wildcard. – mgarciaisaia Jul 04 '14 at 17:41
  • This was a great answer. I found that my bundle ID was wrong and it was set to automatic. – Mike Owens Oct 21 '14 at 20:19
37

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.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • It didn't worked for me. Any other pointers. please look at this : http://stackoverflow.com/questions/25679750/no-valid-aps-environment-entitlement-string-found-for-application-while-debugg – Ajeet Sep 05 '14 at 11:40
28

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.
Oren
  • 5,055
  • 3
  • 34
  • 52
  • Oddly enough, this worked for me! I refreshed until it stopped updating, then I cleaned my build folder, then restarted XCode. This was after I verified the Bundle ID in the provisioning profile and the built app (Where it matched in both, further perplexing me). – Sean Michael Dorian Sep 10 '15 at 15:28
  • 1
    There is no Refresh button(?) anymore – Gerry Jun 18 '19 at 19:03
9

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!

jasonpurdy
  • 151
  • 2
  • 6
9

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.

sudo
  • 5,604
  • 5
  • 40
  • 78
9

I fixed this problem by enabling push notification capability.

Select Project > Target > Capabilities > Turn On Push notifications

check out the image

Community
  • 1
  • 1
error-404
  • 229
  • 4
  • 18
3

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

Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
2

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

Louis Cremen
  • 764
  • 7
  • 12
2

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.

KevinH
  • 2,034
  • 1
  • 12
  • 12
2

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.

bittu
  • 683
  • 7
  • 24
0

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.

user3344717
  • 161
  • 3
  • 5