Good afternoon, habrazhitel.
Everything is probably aware that in iOS there is such a useful service as Push notifications. I was also aware, but I have not yet encountered its real use, I did not even know that there are so many nuances with it. In this topic, you will learn two aspects about this service: what to do in xCode, and how to send the push messages yourself through a php server.
Under the cat described how it all works.
I faced Push recently when I developed my new application,
Family Expenses . There I was going to use Push to notify the user about the synchronization request.
Push notifications are of two types: local and remote. Local in this article will not be considered. They have a fairly simple implementation mechanism and are described in detail in the references.
The mechanism for remote push notification is as follows.
')
- When the application is started, the didRegisterForRemoteNotificationsWithDeviceToken method is called, which, if connected to the Internet, returns the 64 character string Token. With this building there is a nuance: for some reason it comes with the symbols "<" and ">" at the beginning and end and spaces in the middle. Therefore it is worth cleaning the string from these characters.
- The token must be sent to your server that stores it. In fact, it is a unique address of the application and device. Through it then sending Push notifications is conducted. The code below 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. - As required, our server initializes sending Push notifications, the destination address of 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 for our server does not fit completely. It needs to be further processed and linked with your developer certificate.
Detailed step by step instructions can be found here .
To send a message on the server, in our case hosting with PHP, you need to install a certificate that 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. The most important thing - start up with a half kick.
code.google.com/p/apns-phpThere is also a sample project for xCode with the mechanism for processing incoming Push messages.
If in general - this is almost everything.
Now the nuances that I encountered.For sending messages to Apple, there are two servers:
ssl://gateway.push.apple.com:2195
ssl://gateway.sandbox.push.apple.com:2195
The first is used for completed products, and the second is for testing, the sandbox.
If you are testing the application in Debug mode and with the Developer provision profile - then when testing on the server, use the sandbox. If you make Ad Hoc release, that is, you sign the application with a certificate for distribution - then the sandbox will not work anymore. I am writing about this because I myself for a long time could not understand why it does not work.
There is also a special mechanism for cleaning the database of tokens 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, too, there are mechanisms for this.
I hope that I simply described the mechanism.
UPD.Just experimented. It turned out that Token is one for a specific device, and or maybe for one developer. Because with different bundle ID I get the same token. But which application to send - this determines the certificate that is on the server.