Apple Push Notification is not delivered

15,310

I had similar problem, for me the key thing I didn't know is that the device token is different for dev and prod (for the same device).

I was using the device token I got when testing in dev to test in prod. This was working fine in dev, but when I switched to prod I kept the same device token in my python script (assuming, wrongfully, that the device token would be the same for the same device) but the actual device token registered in prod was different.

In practice this wouldn't happen as the device token is sent to the server, but when I was testing I was using hard coded device token as I didn't want to involve the server.

Hopefully this will save someone some frustration.

Share:
15,310

Related videos on Youtube

Lionel Chan
Author by

Lionel Chan

Full stack software engineer. Main framework Laravel, Angular, React.

Updated on June 04, 2022

Comments

  • Lionel Chan
    Lionel Chan almost 2 years

    I know it's a little bit flooded with Apple's Push Notification Service (APNs) issues all over StackOverflow so I actually did a lot of researches before coming up my question.

    Basically, I have followed the apns-php's CertificateCreation, some tutorial like on mobiforge, and some from Matthijs Hollemans. Still, I can't get any notification posted onto my iPad 2.

    Basically these are the steps I have done:

    1. I followed the CertificateCreation tutorial, I came out with server_certificates_bundle_sandbox.pem, entrust_root_certification_authority.pem.
    2. Followed the tutorial specified in mobiforge blog, I successfully created the App ID, provisioning profile and linked them together nicely.

      • Description: APNTest
      • Bundle Seed ID: Use Team ID
      • Bundle Identifier: com.learn.APN
    3. Then I enable the Development Push SSL Certificate (only for now) using the same certificate request I have used just now to get the certs, and I download it and install it into my keychain.

    4. I downloaded my provisioning profile in .mobileprovision file and loaded it into Xcode's Organizer under my iPad 2 device.

    5. Next I create a simple application with only these codes:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
          // Override point for customization after application launch.
          return YES;
      }
      
      - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
      {
          NSLog(@"%@", deviceToken);
      }
      
      - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
      {
          NSLog(@"Error: %@", error);
      }
      
    6. Then I configure the build parameters:

      • Targets > Info > Build Identifier: net.learn.APN
      • Targets > Build Settings > Code Signing Identity > (I chose the provisioning profile I have installed just now, and it automatically selects the identical profiles for my Debug and Release)
    7. I build and run the application on my iPad 2. My application requesting for Push Notification and after I approved it, the Token ID appeared in my console:

      2012-01-19 12:43:26.345 TestAPN[578:707] <3cb08612 9392996b 5332d2fc 5adce03d 994f8959 d7a2ac38 de7bed52 c8e0778e>
      
    8. I used apns-php, changed the device ID (raw 64 hexa-values), then run the sample_push.php. Script ran successfully, but my device is not receiving any notification

    9. Then I try to use the SimplePush.php. A "Message successfully delivered" appeared, but my iPad still doesn't receive any notification from APN

    Question is, which steps have I done wrongly? I'm pretty newbie in this area, and it is actually a requirement for my project that the server need to send out messages to our own applications. We are aware of the third party softwares that send APN on behalf of you, but we would like to implement one ourselves.

    I also came across the "APN Feedback Service" and I written this (base on the SimplePush) but sadly it's empty. What should I do now?

    <?php
    // Put your private key's passphrase here:
    $passphrase = '';
    
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'server_certificates_bundle_sandbox.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    
    // Open a connection to the APNS server
    $fp = stream_socket_client(
            'ssl://feedback.sandbox.push.apple.com:2196', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    
    if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);
    
    echo 'Connected to APNS' . PHP_EOL;
    
    while (!feof($fp)) {
            $data = fgets($fp, 1024);
            var_dump(unpack("N1timestamp/n1length/H*devtoken", $data));
    }
    // Close the connection to the server
    fclose($fp);
    

    Note

    1. I am aware of the differences between development/production certs.
    2. iPad is not jailbroken, running iOS 5.0.1
    3. Other App Store apps receives notifications normally
    4. XCode 4.2.1, Lion 10.7.2

    I hope I'm not duplicates any of the questions here.. ;)

    EDIT

    When I run php simplepush.php then php feedback.php, I received a feedback with my device ID. My App is still in the iPad, so does it mean my device ID is wrong?

    [root@ip-xx-xx-xx-xx SimplePush]# php feedback.php
    Connected to APNS
    array(3) {
      ["timestamp"]=>
      int(1326962028)
      ["length"]=>
      int(32)
      ["devtoken"]=>
      string(64) "3cb086129392996b5332d2fc5adce03d994f8959d7a2ac38de7bed52c8e0778e"
    }
    

    Thanks

    EDIT

    After much struggling and redoing all the steps written by Matthijs Hollemans, I finally get it working. So the problem lies within the incorrect PEM file generated... hmm

    • nevan king
      nevan king over 12 years
      You might as well put up your answer. Anything that helps is good.
    • Lionel Chan
      Lionel Chan over 12 years
      Probably. I will post my steps up here later for others who might need it ;) Basically is the same as the one in the tutorial just some modifications base on XCode 4.2.1 and new Apple cert interface