iOS push messages are a very useful feature, and many applications have this capability to become the core of the data flow for real-time applications. So how do we use PHP to do the push service for iOS? The following article will give you a detailed explanation.
The IOS message push mechanism can be used to realize and discuss the IOS message push mechanism.
First, you need a PEM certificate that needs to be consistent with the development-time signature. The specific generation of the PEM certificate method is as follows:
1. Login to the IPhone Developer Connection Portal (http://developer.apple.com/iphone/manage/overview/index.action) and click App IDs
2. Create an Apple ID. Wildcard IDs cannot be used with push Notification Services. For example, Com.itotem.iphone
3. Click "Configure" next to the Apple ID, generate a signature upload according to the steps in the wizard, and then download the generated license.
4. Double-click the. cer file to import your aps_developer_identity.cer into the keychain.
5. Start the Keychain assistant on the Mac, and then select the Certificates category in the login keychain. See an extensible option "Apple Development Push Services"
6. Extend this option and then right-click "Apple Development push Services" > Export "apple Development push Services ID123". Save as a apns-dev-cert.p12 file.
7. Extended "Apple Development Push Services" to the "Private Key" to do the same operation, save as a apns-dev-key.p12 file.
8. Convert these files to PEM format via a terminal command: OpenSSL pkcs12-clcerts-nokeys-out apns-dev-cert.pem-in APNS-DEV-CERT.P12
OpenSSL pkcs12-nocerts-out apns-dev-key.pem-in APNS-DEV-KEY.P12
9. Finally, you need to synthesize the keys and license files as APNS-DEV.PEM files, which need to be used when connecting to APNs:
Cat Apns-dev-cert.pem Apns-dev-key-noenc.pem > Apns-dev.pem
The PHP code is as follows:
- <?php
- $deviceToken = $_post[' token ']; Get token
- $body = Array ("APS" => Array ("Alert" => ' message ', "badge" => 2, "sound" => ' Default ')); Push way, including content and sound
- $ctx = Stream_context_create ();
- If you have problems finding a PEM path on a Windows server, the path is modified in such a way that:
- $pem = DirName (__file__). ‘/’ . ' Apns-dev.pem ';
- Linux server directly write the path of the PEM
- Stream_context_set_option ($ctx, "SSL", "Local_cert", "Apns-dev.pem");
- $pass = "123123";
- Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $pass);
- There are two servers to choose from, if it is development testing, select a second sandbox server and use Dev's PEM certificate, if it is a release, use the product's PEM and choose a formal server
- $fp = Stream_socket_client ("ssl://gateway.push.apple.com:2195", $err, $errstr, Stream_client_connect, $ctx);
- $fp = Stream_socket_client ("Ssl://gateway.sandbox.push.apple.com:2195″, $err, $errstr, Stream_client_connect, $ CTX);
- if (! $fp) {
- Print "Failed to connect $err $errstrn";
- Return
- }
- Print "Connection ok\n";
- $payload = Json_encode ($body);
- $msg = Chr (0). Pack ("n", 32). Pack ("h*", Str_replace (",", $deviceToken)). Pack ("n", strlen ($payload)). $payload;
- Print "Sending message:". $payload. "\ n";
- Fwrite ($fp, $msg);
- Fclose ($FP);
- ?>
And then
Here the certificate is ready, and next, we'll create a new test project in Xcode, and note that the bundle identifier for the Setup project must be the same as the app ID built above.
Add code to Didfinishlaunchingwithoptions
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
[Self.window makekeyandvisible];
[[UIApplication Sharedapplication] Registerforremotenotificationtypes:uiremotenotificationtypebadge Uiremotenotificationtypesound Uiremotenotificationtypealert];
return YES;
}
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) PToken {
NSLog (@ "Regisger success:%@", PToken);
}
-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo{
Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Push notification message:@" message "delegate:selfcancelbuttontitle:@" Cancel "Otherbuttontitles:nil, nil];
[Alert show];
[Alert release];
}
-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error {
NSLog (@ "Regist fail%@", error);
}
Next we visit http://localhost/push/push.php
The iOS device will receive a push message.
In addition, the method of removing the mark is to add in the Viewdidapper
int badge = [UIApplication sharedapplication].applicationiconbadgenumber;
if (Badge > 0)
{
badge--;
[UIApplication sharedapplication].applicationiconbadgenumber = badge;
}