Push notifications in iOS apps

    Good afternoon, the Guardian.
    Everyone probably knows that in iOS there is such a useful service as Push notifications. I was also aware, but until I came across its real use, I did not even know that there were so many nuances with it. In this topic, you will learn two aspects about this service: what you need to do in xCode, and how to send push messages yourself via php server.
    Under the cut how it all works is described.

    I came across Push recently when I was developing my new application - Family Expenses . There I was going to use Push to notify the user of a synchronization request.

    Push notifications come in two forms: local and remote. We will not consider local ones in this article. They have a fairly simple implementation mechanism and are described in detail in the references.

    The remote push notification mechanism is as follows.

    1. When the application starts, the didRegisterForRemoteNotificationsWithDeviceToken method is called which, if there is an Internet connection, returns a 64 character Token string. There is a nuance with this construction: for some reason, it comes with the characters "<" and ">" at the beginning and end and spaces in the middle. Therefore, it is worth cleaning the line from these characters.
    2. The token must be sent to your server, which stores it. In fact, it is the unique address of the application and device. Through it, then push notification is sent. The code below just presents the mechanism described in paragraphs 1 and 2.

      - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken  
      {  
          NSMutableString *tokenString = [NSMutableString stringWithString:  
                                          [[deviceToken description] uppercaseString]];  
          [tokenString replaceOccurrencesOfString:@"<"  
                                       withString:@""  
                                          options:0  
                                            range:NSMakeRange(0, tokenString.length)];  
          [tokenString replaceOccurrencesOfString:@">"  
                                       withString:@""  
                                          options:0  
                                            range:NSMakeRange(0, tokenString.length)];
          [tokenString replaceOccurrencesOfString:@" "  
                                       withString:@""  
                                          options:0  
                                            range:NSMakeRange(0, tokenString.length)];     
          NSLog(@"Token: %@", tokenString);  
          if (tokenString) {
              [[NSUserDefaults standardUserDefaults] setObject:tokenString forKey:@"token"];
          }
          NSString *urlFormat = @"http://your.domain.com/regDevice.php?deviceToken=%@";  
          NSURL *registrationURL = [NSURL URLWithString:[NSString stringWithFormat:  
                                                         urlFormat, tokenString];  
          NSMutableURLRequest *registrationRequest = [[NSMutableURLRequest alloc]  
                                                      initWithURL:registrationURL];  
          [registrationRequest setHTTPMethod:@"PUT"];  
          NSURLConnection *connection = [NSURLConnection connectionWithRequest:registrationRequest  
                                                                      delegate:self];  
          [connection start];  
          [registrationRequest release];
      }   
      

      On the application side, this is almost everything, apart from the mechanism for processing an incoming message.
    3. As necessary, our server initializes the sending of Push notifications, the recipient address in which is the Token. To do this, the server must have a Push certificate that can be obtained from developer.apple.com in the Provisioning Portal -> App IDs -> Configure section opposite your App ID. I will not describe this process in detail, because there is a step-by-step instruction on how to create a certificate.
      But that certificate is not completely suitable for our server. It must also be processed and combined with your developer certificate.
      Detailed step-by-step instructions can be found here .


    To send a message to the server, in our case hosting with PHP, you need to install the certificate, which is generated according to the instructions in the link above.
    On my server, I used ApnsPHP, an open source class for interacting with the Apple Push Notification service.
    Everything is very simple and convenient. Most importantly, it starts with a half-kick. code.google.com/p/apns-php
    There is also an example project for xCode with a mechanism for processing incoming Push messages.

    If in general - that’s practically all.

    Now the nuances that I encountered.
    Two servers were provided for sending messages to Apple: The first is used for completed products, and the second for testing, the sandbox.
    ssl://gateway.push.apple.com:2195
    ssl://gateway.sandbox.push.apple.com:2195


    If you are testing the application in Debug mode and with the Developer provision profile, then use sandbox when testing on the server. If you make an Ad Hoc release, that is, sign the application with a distribution certificate, then sandbox will not work. I am writing about this because I myself could not understand for a long time why it does not work.
    There is also a special mechanism for cleaning the token database from addresses that are no longer valid. For example, if the application was removed from the device.
    To do this, you can use ApnsPHP there are also mechanisms for this.

    I hope that I described the mechanism quite simply.
    UPD.
    Just experimented. It turned out that the Token is one for a particular device, and or maybe for one developer. Because with different bundle IDs, I get the same token. But to which application to send - this determines the certificate that is on the server.

    Also popular now: