Sahil Tyagi
Sahil Tyagi

Reputation: 363

Unable to get push notifications on all devices

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];
            }
        }
}

Upvotes: 0

Views: 2201

Answers (3)

Sahil Tyagi
Sahil Tyagi

Reputation: 363

I replaced the following code:

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];

By this:

[devToken retain];
    NSLog(@"~~~~devToken=%@",devToken); 
    NSString *dt = [[devToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    //     //DeviceToken = dt;
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:dt forKey:@"DeviceToken"];
    [def synchronize];

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!!

Upvotes: 1

RickJansen
RickJansen

Reputation: 1713

  1. Are you setting the "expiry" to 0 in the APN message? If you set it to 0 push messages are try-once-and-forget, else Apple may try to deliver them till the time specified.

  2. The other thing to check might be to be absolutely certain no device id's from your development test runs end up in the list of device id's you are actually going to send push messages in production. One faulty device id and Apple will cease the SSL connection and not process any more APN messages.

Upvotes: 1

PJR
PJR

Reputation: 13180

1 ) There is no guarantee that push notifications will actually be delivered, even if the APNS server accepted them.

2 ) As far as your server is concerned, push notifications are fire-and-forget; there is no way to find out what the status of a notification is after you’ve sent it to APNS. The delivery time may also vary, from seconds up to half an hour.

3 ) Also, the user’s iPhone may not be able to receive push notifications all the time. They could be on a WiFi network that does not allow connections to be made to APNS because the required ports are blocked. Or the phone could be turned off.

4 ) APNS will try to deliver the last notification it received for that device when it comes back online, but it will only try for a limited time. Once it times out, the push notification will be lost forever!

Upvotes: 2

Related Questions