Determine if device token is sandbox or distribution
Asked Answered
H

4

9

Is there a way to determine if a device token is sandbox or distribution? We are testing and the application is sometimes signed with a development certificate and others are signed with an ad hoc certificate(distribution certificate). This is because we are passing the application around to some of the 100 provided ad hoc test devices, and also building development signed apps to our devices. Because sending a push notification requires that we select the appropriate push server and pem file, it would be great to be able to determine if the token is sandbox or distribution to send the notifications in the appropriate way so that the push notification succeeds. We must sometimes use the distribution profile to sign our applications, so testing the push notification system requires us to deliver these notifications properly.

Heteroousian answered 4/5, 2011 at 7:4 Comment(0)
H
7
  1. Open project build setting
  2. Go to preprocessing settings
  3. Under "Preprocessor macros not used in precompiled headers" (assuming you are not branching code in a .pch file), add TOKEN_ENV_SANDBOX=0 under Release and TOKEN_ENV_SANDBOX=1 under Debug and Ad Hoc.
  4. In your code wherever just use the compiler directive

#if !TOKEN_ENV_SANDBOX
NSLog(@"TOKEN_ENV==PRODUCTION");
#endif

#if TOKEN_ENV_SANDBOX
NSLog(@"TOKEN_ENV==SANDBOX");
#endif

EDIT: Corrected an issue above.

Heteroousian answered 14/5, 2011 at 18:39 Comment(8)
How to Add Adhoc To my "Preprocessor macros not used in precompiled headers"Gauleiter
May I know if this is still valid? I notice some of the users are identified as sandbox token, with the same release from App Store.Pyrene
Why for Ad Hoc, the TOKEN_ENV_SANDBOX is 0? And for Debug, TOKEN_ENV_SANDBOX=1? Is there a mistake?Pyrene
@Pyrene good catch. very old answer and I'm surprised no one caught it :)Heteroousian
Additionally the "new" way if you are using HTTP/2 is a single token that can be used for both sandbox and production APNHeteroousian
My Typo too. Should be "And for Release, TOKEN_ENV_SANDBOX=1". By the way, isn't Ad-hoc use production?Pyrene
@Heteroousian thanks for your info on http/2 ! I am looking at it. So 1 APNS url address to send both production / development ? :)Pyrene
No different APNS urls, but you can now generate one .cer certificate from Apple that can be used to sign notifications for both development and production. developer.apple.com/library/ios/documentation/IDEs/Conceptual/…Heteroousian
M
1

I read all answers above and they are all correct but do not answer the basic question: "Is device token sandbox or distribution?".

It is because they detect build configuration, not token quality.

After spending several days in resolving this issue, I got to the straigt-forward solution:

Test token you got with real Apple Push Notification Server. You may use simple app that will talk to the server and you just need to configure it.

I did use this simple app "Easy APNs Provider" for macOS or any else.

https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12

My core issue in macOS app was that I keep getting production token in both Debug and Release configurations.

When you detect what was an issue, you may invalidate certificates to be 100% sure it will not be compromised.

Medullary answered 5/2, 2017 at 11:9 Comment(0)
L
0

Here is what Apple has to say

You can determine in Xcode which environment you are in by the selection of a code-signing identity. If you see an “iPhone Developer: Firstname Lastname” certificate/provisioning profile pair, you are in the sandbox environment. If you see an “iPhone Distribution: Companyname” certificate/provisioning profile pair, you are in the production environment.

Lalittah answered 21/2, 2013 at 3:36 Comment(0)
O
-1

Add an Preprocessor Macro to your Target's Build Settings under Apple LLVM 7.0 - Preprocessing. Then under Debug add something like:

isRunningInDevModeWithDevProfile=1

Then in your .pch, you can do something like this:

// AZ - 01282016 - Determine which environment we are running in for APNS
# ifdef isRunningInDevModeWithDevProfile
#     define isAPNSSandbox YES
#else
#    define isAPNSSandbox NO
#endif

And then where you need to check it in your code, you can do this:

NSString *ifAppIsRunningFromXcodeUsingNonReleaseProfile;

if (isAPNSSandbox) {
    ifAppIsRunningFromXcodeUsingNonReleaseProfile = @"dev";
} else {
    ifAppIsRunningFromXcodeUsingNonReleaseProfile = @"prod";
}

We pass this value back to our APNS server so that it knows which path to take when calling Apple's APNS.

The reason I suggest this is that in this case, if you leave the preprocessor macro undefined in the non Debug targets, this will not cause an error when you try to build it.

Sure, there are solutions with a better form, but this is is a rather safe and quick way to get this functionality up and running.

Octopus answered 28/1, 2016 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.