I am using the APNS for receiving push notifications in my application.
The problem is that I am getting the same notifications on some devices but not all. What could the problem be here since I have been trying for about 15 days to solve this issue with no success. Th device token is updating successfully. Because had it not been then I wouldn’t have been getting notification on any device. But the strange thing is I am getting it on half the devices. Please help!!
Here is the code for registering and receiving notifications. I don’t have any code for server side. But as I said that the notifications are working on some devices. On android too they are working.
I have 3 devices here with me and its working on two of them.
iPad2:5.0.1
iPodTouch:4.3.3
Its not working on another iPod touch that I have on version:5.1
Also its showing successfully registered for APNS in all devices. But not sending notifications on some. What can the issue be? Is there something that I am missing?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
deviceToken = [devToken retain];
NSLog(@"Registered for APNS %@", deviceToken);
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSMutableString *dev = [[NSMutableString alloc] init];
NSRange r;
r.length = 1;
unsigned char c;
for (int i = 0; i < [deviceToken length]; i++)
{
r.location = i;
[deviceToken getBytes:&c range:r];
if (c < 10) {
[dev appendFormat:@"0%x", c];
}
else {
[dev appendFormat:@"%x", c];
}
}
[ud setObject:dev forKey:@"DeviceToken"];
[ud synchronize];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Failed to register %@", [error localizedDescription]);
deviceToken = nil;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
for(int i=0;i<[viewControllers count];i++)
{
if([[viewControllers objectAtIndex:i] isKindOfClass:[Confirmation class]])
{
Confirmation *map = (Confirmation*)[[self.navigationController viewControllers] objectAtIndex:i];
[map setFinalInfo];
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:i] animated:YES];
}
}
}
I replaced the following code:
By this:
and its working fine now!!
The problem was it wasnt updating the device toekn correctly for some devices which I am not sure why as it was working well for some.
Thanks guys!!