This article describes how to set up an iPhone Push server instance for PHP to operate an iPhone Push server instance for PHP. Adding the Push function to an application is very helpful for users to obtain information in a timely manner. I have previously introduced the iPhone's Push (Push Notification) function principle analysis, it mentioned that to add a push function for your App, developers must first build a push server. The following describes how to create a PHP Push server instance for the iPhone.
This article describes how to create a Push server instance for the iPhone in PHP. Adding the Push function to an application is very helpful for users to obtain information in a timely manner. I have previously introduced the iPhone's Push (Push Notification) function principle analysis, it mentioned that to add a push function for your App, developers must first build a push server. The following describes how to build a php push server for the iPhone application.
0. install XCode on Mac OS X and connect to a normal iPhone to maintain a peaceful attitude.
Basic settings for APP development
1. create the app id and Device in the iPhone Provisioning Portal.
2. Generate the Certificate Request CertificateSigningRequest. certSigningRequest in the Keychain Access. app (menu> Keychain Access> Certificate Assistant> Request a Certificate From a Certificate Authority ...).
3. Request a Certificate from iPhone Provisioning Portal> Certificates (click Request Certificate to upload CertificateSigningRequest. certSigningRequest ).
4. after the request is complete, download the certificate File (pai_identity.cer) and double-click it to import it to the Key Chain.
5. on the iPhone Provisioning Portal> Provisioning, create a Profile, select the specified app id and Devices, and generate the Profile.
6. download the generated Profile as * _ profile. mobileprovision, double-click the File, and load the profile to the iPhone.
Push Notification service settings
7. on the iPhone Provisioning Portal> App IDs, select the App ID of the Push service to go to Configure.
8. confirm the Enable for Apple Push Notification service, configure the Development Push SSL Certificate, and upload the Certificate request generated in step 1.
9. download the generated aps_developer_identity.cer to complete the Push service configuration.
10. double-click aps_developer_identity.cer and save it to the Key Chain.
Generate the certificate file required by php Push Notification sender
11. in the Keychain Access. app, select the new certificate (Apple Development Push Services *), export it to the desktop, and save it as Certificates. p12.
12. run the following command:
Openssl pkcs12-clcerts-nokeys-out cert. pem-in Certificates. p12
Openssl pkcs12-nocerts-out key. pem-in Certificates. p12
Openssl rsa-in key. pem-out key. unencrypted. pem
Cat cert. pem key. unencrypted. pem> ck. pem:
13. create a View-based Application project in $ PROJECT_NAMEAppDelegate.m:
A. paste the following code:
-(Void) applicationDidFinishLaunching :( UIApplication *) app {
// Other setup tasks here ....
[Window addSubview: viewController. view];
[Self alertNotice: @ "" withMSG: @ "Initiating Remote Noticationss Are Active"
CancleButtonTitle: @ "OK" otherButtonTitle: @ ""];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
}
-(Void) application :( UIApplication *) app
DidRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
// NSLog (@ "devToken = % @", deviceToken );
[Self alertNotice: @ "" withMSG: [NSString stringWithFormat: @ "devToken = % @", deviceToken]
CancleButtonTitle: @ "OK" otherButtonTitle: @ ""];
}
-(Void) application :( UIApplication *) app
DidFailToRegisterForRemoteNotificationsWithError :( NSError *) err {
NSLog (@ "Error in registration. Error: % @", err );
[Self alertNotice: @ "" withMSG: [NSString stringWithFormat: @ "Error in registration.
Error: % @ ", err] cancleButtonTitle: @" OK "otherButtonTitle: @" "];
}.
-(Void) alertNotice :( NSString *) title withMSG :( NSString *) msg cancleButtonTitle :( NSString
*) CancleTitle otherButtonTitle :( NSString *) otherTitle {
UIAlertView * alert;
If ([otherTitle isEqualToString: @ ""])
Alert = [[UIAlertView alloc] initWithTitle: title message: msg delegate: self
CancelButtonTitle: cancleTitle otherButtonTitles: nil, nil];
Else
Alert = [[UIAlertView alloc] initWithTitle: title message: msg delegate: self
CancelButtonTitle: cancleTitle otherButtonTitles: otherTitle, nil];
[Alert show];
[Alert release];
}
B. add in-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {method
[Self alertNotice: @ "" withMSG: @ "Initiating Remote Noticationss Are Active" cancleButtonTitle: @ "OK" otherButtonTitle: @ ""];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | uiremotenotiftypetypebadge | uiremotenotiftypetypesound)];
14. project settings
A. Targets> $ APP_NAME> context menu> Properties> Identifier
Modify
Identifier
Is
App ID
B. Targets> $ APP_NAME> context menu> Build> Code Signing Identifier> Any iPhone OS Device
Specify the iPhone Developer as a development machine. after compilation and running, the device token is displayed on the iPhone.
Php Push Notification sender code:
DeviceToken = "device token ";
$ Body = array ("aps" => array
("Alert" => 'Message', "badge" => 1, "sound" => 'received5. caf '));
$ Ctx = stream_context_create ();
Stream_context_set_option ($ ctx, "ssl", "local_cert", "ck. pem ");
$ Fp = stream_socket_client
("Ssl: // gateway.sandbox.push.apple.com: 2195", $ err, $ errstr, 60, 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 *", $ deviceToken ). pack ("n", strlen ($ payload )). $ payload;
Rint "sending message:". $ payload. "\ n ";
Fwrite ($ fp, $ msg); fclose ($ fp );
?>
How to create a PHP Push server instance for iPhone
1st floor CJSen 2011-12-15
In iphone push, if I want to send the same message to many devices, is there any interface or method to say: I sent all device IDs and a message from the server to apns. the Apple server automatically sends messages to each device, instead of sending messages one by one with a device ID on the server;