Not able to get Device Token in ios 8 - ios

i am not getting into the method
didRegisterForRemoteNotificationsWithDeviceToken
Actually i want to get device token and i have tried to follow this question Get Device Token in iOS 8 but still facing the same problem
.Help me to come out of this problem

Try this.
In your method.
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
use this code
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
And then Put log or breakpoints that your
didRegisterForRemoteNotificationsWithDeviceToken
get called or not.
And if your app is properly registered for remote-notification settings and yet you are not getting device token then
check
didFailToRegisterForRemoteNotificationsWithError
method that will tell you which error you have.
NOTE : device-token will return only when you run on device. OS Simulator doesn't support push notifications
And then also if you are not able to get this method called, then check out this tutorial
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
And follow the steps for creating provisioning profile and certificates. I tried with my existing certificate. After you run your app with valid profile as explained in this tutorial series, you will able to get that method called.

There are different way to get device token in iOS 8.
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

The problem was that there is some kind of restriction in my company network so when i used a wifi device which was not on company network my code worked fine.

Related

Device token not generating iOS

I am facing issue while generating the device token with the real device.
I am debugging device and device token is not generating.
Sometimes it works and sometimes not.
Please let me know what could be issue.
Thanks
Did you put below in didFinishLaunchingWithOptions of AppDelegate.m?
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
NSLog(#"ios8 app");
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
NSLog(#"lower ios8 app");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
As of now, I, too, am having trouble getting device tokens.
My projects stopped generating device tokens when it was "erased" and "installed" around 10 hours ago.
Also in the Korean iOS Developers forum, some people have been reporting problems with APNS tokens not generating in the past 10 hours.
There may be something wrong with some of the sandbox APNS servers.
Last checked time
2016-04-27 22:43 PM +0900 GMT : No device token, Push Notification Not Arriving.
Add this method,
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
print("application:didFailToRegisterForRemoteNotificationsWithError: %#", error)
}
You will get answer, why token is not generated.
#GopalDevra could you be more clear? Anyway, I have this code for Parse, maybe it's not your case, but you can get the idea.
You can use didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate.m like:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = #[ #"global" ];
[currentInstallation saveInBackground];
}
Edited
Have you put this in didFinishLaunchingWithOptions?
// - Push notifications
// -- Register for Push Notitications
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
//-- Set Notification
if ([application respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
//--- your custom code

Receiving duplicate push notification ios9

I am receiving the same push notification twice in iOS9, although it is working fine in iOS8.
I have used the following code to register with push notifications:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
// use registerUserNotificationSettings
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:( UIUserNotificationTypeSound | UIUserNotificationTypeAlert|UIUserNotificationTypeBadge) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// use registerForRemoteNotifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeBadge)];
}
#else
// use registerForRemoteNotifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
I had this problem in several apps, and looks like duplicates appear if you call registerUserNotificationSettings: more than 1 time.
More details in this answer:
https://stackoverflow.com/a/35064911/4495995
It is apparently an Apple issue. I've faced the same issue many times across apps. https://forums.developer.apple.com/thread/13414
From iOS 9 every time when you uninstall and than re-install the app again a new device token has assigned this might be the reason that you receives the multiple push notifications.
Actually I read out from one forum, they provide the solution that when you generating a payload that add one extra custom any random value so that each payload has some unique value. in my case in vb.net I am using DateTime.Now.ToString("MMddyyyyHHmmssfff") to add a unique time stamp with milliseconds. I hope its work I implemented this but not tested so far.
I am using this and this is working fine in Ios9 also, please try it. Add this in your didFinishLaunchingWithOptions:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
Method for call is
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#"<>"]];
self.AppDeviceToken=[token stringByReplacingOccurrencesOfString:#" " withString:#""];
}
First check your database and make sure you didn't get the device token twice, quite possible that you have duplicate entries of the same token.
Secondly, If you install/uninstall the app within 3 to 4 days its possible that you get the notification twice or even thrice.
Solution: Please uninstall the app for a week if possible than install the app again.
Thank You.

didReceiveRemoteNotification NOT called on IOS 8.2 / Xcode 6.2

My app not receveing push notification.
I use on IOS 8.2 and registration is:
if([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
I see the server send notification but my device not received...
How can I resolve?

Registering for Parse notifications iOS7 and iOS8, compiler warnings

I have coded my app to receive Parse notifications in iOS8 and recently discovered these wouldn't work in iOS& so had to change my code to as follows in AppDelegate.m:
viewDidLoad:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
NSLog(#"Requesting permission for push notifications...iOS8"); // iOS 8
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
NSLog(#"Registering device for push notifications..."); // iOS 7 and earlier
[UIApplication.sharedApplication registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
and:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current Installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
I tested on both version of iOS and it seems to be working fine. However if I change the deployment target in Xcode to iOS8 or above I get these compiler warnings:
AppDelegate.m:62:10: 'UIRemoteNotificationTypeAlert' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
AppDelegate.m:62:42: 'UIRemoteNotificationTypeBadge' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
AppDelegate.m:63:10: 'UIRemoteNotificationTypeSound' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
/AppDelegate.m:61:42: 'registerForRemoteNotificationTypes:' is deprecated: first deprecated in iOS 8.0 - Please use registerForRemoteNotifications and registerUserNotificationSettings: instead
Is it normal to see these warnings and is it just because I'm working in a higher deployment target? Or do I have to change the code somewhere? Seems odd that I would have to see these warnings. Any pointers would be really appreciated. Thanks!
If you changed the deployment target to iOS 8, so no need for checking for iOS 8 or not (Before iOS 8 as your app will work in iOS 8 devices only), thats why it gives you these warnings. So just register like this:
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
Yes, That method is deprecated in iOS8 but you can check condition on iOS7 and iOS8 by these lines of code to manage deprecation:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
and addition to that add these line of code below it.
if ([launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
and to register device on parse.com please check once your applicationKey and ClientKey is correct other than that your code block for register device is correct, but it won't affect that deprecations of iOS at all.

didRegisterForRemoteNotificationsWithDeviceToken not called on iOS7

I regularly ask for the notification token with:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
yet the didRegisterForRemoteNotificationsWithDeviceToken callback is consistently called on my iOS 8 iPad and never on my iOS 7 iPhone (not even the didFailToRegisterForRemoteNotificationsWithError one), not even by deleting the app and reinstalling it: nor the alert is displayed. I checked both the certificates and the settings in my iPhone but all seems in order, as well test all the suggestions in SO and all over the internet but nothing seems to fix it. Any more suggestion?
This can be due to a permission deny. Application uninstall does not reset this permission.
In order to verify whether this permission is denied do the following(iOS7):
[[UIApplication sharedApplication] enabledRemoteNotificationTypes] != UIRemoteNotificationTypeNone;
If the permission is denied you will have to do the following steps:
Reset permissions in device settings
Remove the app
Move device clock two days ahead
Restart the phone
Install application and run again
Don't forget to set the clock back after notifications permission popup.
Check the following setting in your Xcode project target:
Target project>Capabilities>Push Notification
Enable the Push Notification and check if you have added the 'Push notification' entitlement to your App Id.
i am using following 'Code' and its working for me.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Try this,
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
Hope this helps

Resources