Get Device Token in iOS 8

Get Device Token in iOS 8

Read the code in UIApplication.h.

You will know how to do that.

First:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

add Code like this

#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

if you not using both Xcode 5 and Xcode 6 ,try this code

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}

(Thanks for @zeiteisen @dmur 's remind)


Second:

Add this Function

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif

And your can get the deviceToken in

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

if it still not work , use this function and NSLog the error

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

Could not get Device Token for Device running iOS 8+ (APNS)

apns sandbox seems down today. It responds with code 200 to push notifications, but doesn't deliver the push notification to the device.

Note that https://developer.apple.com/system-status/ doesn't show anything anormal, probably because sandbox monitoring isn't very high on their priority.

Push Notifications Cannot get Device Token

Here is the way you should register for remote notifications:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
{
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}

Also if you need to send it to your backend, you first take a look here and extract a string token in didRegisterForRemoteNotificationsWithDeviceToken:: Iphone device token - NSData or NSString

Then use AFNetworking or NSURLSession to send it to your backend API.
Take a look here for example: Send POST request using NSURLSession

MonoTouch Get DeviceToken in iOS8

Simple answer, I was missing the following line of code when registering:

UIApplication.SharedApplication.RegisterForRemoteNotifications();

Adding this line meant that the code entered the RegisteredForRemoteNotifications handler.

So the complete code for registering for notifications is:

var version8 = new Version (8,0);
if (new Version(UIDevice.CurrentDevice.SystemVersion) < version8)
{
var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
}
else
{
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}

how to get Device Token in iOS?

Here is the latest code of swift 4.0, so you can use following code to get device token.

import UserNotifications

if #available(iOS 10, *) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
}
UIApplication.shared.registerForRemoteNotifications()
} else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()

}

How can I get the device token for the iOS 13+ devices in Objective c

I was not getting the Device token for the iPhone with the os greater than 13. I modified the answer from the below link and finally got the answer.

How can I convert my device token (NSData) into an NSString?

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

str2 = [self fetchDeviceToken:deviceToken];

}

- (NSString *)fetchDeviceToken:(NSData *)deviceToken {
NSUInteger len = deviceToken.length;
if (len == 0) {
return nil;
}
const unsigned char *buffer = deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(len * 2)];
for (int i = 0; i < len; ++i) {
[hexString appendFormat:@"%02x", buffer[i]];
}
return [hexString copy];
}

The Device token will assign to the variable str2.

Getting device token in swift

I seemed to fix this by logging into my "apple developer" account and agree to the updated terms of service.

Approximately 1 hour after I did this, I started to receive push notifications again.

Thanks for your help guys!

When application is re-installed How to get devicetoken on iOS8

Looks like you doesn't generate push certificates for production environment, and you have it only for development environment (which is used only for builds launched from xcode, other build types will use production environment). So, just generate push certificates for production environment in developer center and regenerate your ad-hoc provision profile.

Obtain NEW Apple device token?

No, you can't request a new device token. They expire from time to time, and only then will you get a new one (or if you have a different app with a different bundle id, the token will be different).

Create a function to handle didRegister and call that from didRegisterForRemoteNotificationsWithDeviceToken. Then use that function when you need to force the call.

Since users are logging in, pass the information with the device token to the server every time someone logs in and associate the user to the token on the server side.

how to get device token?

Have you enabled push notification in your provisional file ?

Go through the tutorial to check if you have done right

First try sending push notification to device from your mac, as mentioned in tutorial. You will need a pem file on server side that you need to create from iOS Portal :)



Related Topics



Leave a reply



Submit