What is the remote message push feature
Apple adds a message push to iOS and Mac, allowing us to send a message to the app (app) via a backend server, regardless of whether the app is in use, such as the assembling prompt for the mailbox. This service is known as Apple Push Notification service(APNs). There are four roles involved: app, device, APNs, and app backend server (Provider), where apps, background servers, and APNs use Devicetoken to uniquely identify one user.
Workflow for Push services:
- The app registers the push service with the system.
- The device requests Devicetoken from APNs.
- The Devicetoken is returned to the app through the proxy method.
- The app sends Devicetoken to the app backend server (Provider).
- The application background server saves Devicetoken, and then sends the message to APNS when a push notification is required, using Devicetoken to identify the client to be delivered.
- APNs pushes data sent from the background server to the device.
- The device distributes the message to the application.
When using push, you need to create a certificate that supports push notification in the developer center and use the certificate and private key to communicate between the application backend server and the APNs.
Environment configuration
There are a few prerequisites for using the push service:
- Developer account.
- iOS real computer (IPhone, IPad, IPod).
- Background server.
- Internet.
To enable the app to support push services, you need to configure provisioning profile to support push, which is divided into development and production two versions as normal provisioning profile files. We tested it using the development version.
Next, you create an SSL certificate and private key that you use to apply the background server and the APNS server communication.
1. Obtain the certificate request file (CSR) in the Keychain Access tool.
2. Save the request file.
3. Export the private key from the Keychain Access tool, save it as pushkey.p12, and enter the password ABCDE. Do not forget the password, ha, wait for the use.
4. Log in to IOS Dev Center to create the APP ID and *provisioning profile*.
5. When creating a new app ID, be careful to turn on Push Notification.
6. The last app ID looks like this.
7. In this step, although the push service has been started, but further configuration is required, click the Setting button to set.
8. Scroll to the bottom, you need to create an SSL certificate (createdCertificate), and the test environment uses development SSL Certificate.
9. Review the certificate creation steps and instructions, and upload the certificate request file from step 1th .
10. Download the generated certificate, named aps_development.cer.
Here, we have a variety of options to continue.
1. Send push messages using a third-party gadget Pushmebaby simulation app backend server.
2. Build the application background server to send push messages.
Let's try the first method, using Pushmebaby. This is an open source Mac applet, we go directly to GitHub to download the source code, with Xcode open, will APPLICATIONDELEGATE.M in the Sky Devicetoken and certificate location.
-(Id)Init{Self=[SuperInit];If(Self!=Nil){77e231f0 76257e00 eed93ac6 47b52c78 12bae79f 9c9d1c67 4c990589 36c9a235----reserved spacesSelf.Devicetoken=@"";Push content, JSON formatSelf.Payload=@"{\"Aps\":{\ "alert\" : \ " Changsha Camp David Education iOS development training best! \ ",\" badge\ " : 1}} "//get certificate path self. Certificate = [[nsbundle mainbundle ] pathforresource:@ "Aps_development" oftype:@ "CER" ; } return self;}
The Devicetoken is obtained in the following code section.
Be sure to remember to add the certificate file you just added to your project, or you can assign the certificate path directly to Self.certificate.
Next try the second method, we use PHP to send notifications (in fact, running PHP does not need to build another server and download programs, mac default support PHP run, do not trust the command line run php ).
This is a relatively troublesome approach, but it is also a way to use it when it is actually used. We need to further process the private key and certificate files.
1. First process the certificate file and the private key into a single, easy-to-use PEM file, assuming that the CSR, P12, and CER files are placed on the desktop.
$ cd ~/Desktop$ls aps_development.cerCertificateSigningRequest.certSigningRequestPushKey.p12
2. Convert the Aps_development.cer to a PEM file.
$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem$ lsaps_development.cerCertificateSigningRequest.certSigningRequestPushCert.pemPushKey.p12
3. Convert the P12 private key file to a PEM file.
$ openssl pkcs12 -nocerts -out PushKey.pem -in PushKey.p12 Enter Import Password:MAC verified OKEnter PEM pass phrase:Verifying - Enter PEM pass phrase:
4. Synthesize two files into the same one.
$ cat PushCert.pem PushKey.pem > ck.pem$ lsaps_development.cerCertificateSigningRequest.certSigningRequestck.pemPushCert.pemPushKey.pem PushKey.p12
5. Test whether the certificate is valid.
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCert.pem -key PushKey.pem
If it works, it will output a bunch of information and establish a connection, otherwise the connection will not be successfully established.
6. Use PHP to test, download simplepush.php, modify the file and fill in the Devicetoken and password. Run the code at the terminal.
$ php simplepush.php Connected to APNSMessage successfully delivered
The push message was sent successfully.
Code implementation
With these preparations, the iOS development is very simple, and several methods in Uiapplicaton are related to push messages, including local push.
-(BOOL)Application:(UIApplication*)ApplicationDidfinishlaunchingwithoptions:(Nsdictionary*)Launchoptions{Determine if a remote notification is registeredIf(![ApplicationIsregisteredforremotenotifications]){Uiusernotificationsettings*Uns=[UiusernotificationsettingsSettingsfortypes:(Uiusernotificationtypealert|Uiusernotificationtypebadge|Uiusernotificationtypesound)Categories:Nil];[ApplicationRegisterusernotificationsettings:Uns];Registering Remote Notifications[ApplicationRegisterforremotenotifications];}ReturnYES;}Registration successful, return Devicetoken-(void)Application:(UIApplication*)ApplicationDidregisterforremotenotificationswithdevicetoken:(NSData*)Devicetoken{NSLog(@"%@",Devicetoken);}Registration failed-(void)Application:(UIApplication*)ApplicationDidfailtoregisterforremotenotificationswitherror:(Nserror*) error{nslog ( @ "%@" error); } //received push message - (void) application: (uiapplication *) application didreceiveremotenotification: (*) userinfo{NSLog< Span class= "P" > (@ "Remote:%@" userinfo }
You can even use APNs to implement a chat tool, see Resources (4) for details.
Resources
- https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ Chapters/applepushservice.html#//apple_ref/doc/uid/tp40008194-ch100-sw9
- Https://github.com/stefanhafeneger/PushMeBaby
- Http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
- Http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
This document is organized by the Changsha Camp David Education .
Use Pushmebaby to test the app's remote push feature