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.
- Open project build setting
- Go to preprocessing settings
- 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.
- 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.
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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.