Using Nodejs to realize APNs

Source: Internet
Author: User
Tags openssl openssl x509 pkcs12 ssl certificate

APNs (Apple push Notification service) can push notifications to iOS devices, which are visually suggestive of pop-up prompts when an app is running in the background. Compared to Android, there is only one set of programs that makes it less expensive to research. The title is written Nodejs, just because our production environment is NODEJS, I also tune it in other languages, the test library is these three: Node-apns (Nodejs)/Pyapns (Python)/APNs (Ruby). The focus of this article is not on code (because there is little code), but about how to request a certificate in the Apple Developer Center, generate a PEM file, read a PEM file, and a certificate to send a request to the APNs sandbox, and the device receives a notification to handle this sequence of steps. Tips & Preparations

The principle document of the APNS system is written in detail and is not discussed here. Refer to the following figure only as a description.

Nodejs to achieve is the provider (hereinafter called Service) section. 1. You need to apply for an Apple developer account to prepare a real-world device. The simulator has no device Token and cannot test APNs 2. When you receive a reminder, if the program opens in the foreground, you will not see a reminder; 3. If the same reminders for continuous push content fail.

Generate Certificate

Create the app ID at the Apple Developer Center and tick the app Services-push notificationswhen you create it. App ID suffix choose a generic app ID (wildcard app ID) or a fixed app ID (Explicit appID), and if you want to develop multiple apps, you can choose a generic app ID, This article only does an example so create a fixed app Id,name for Dmyz,id for Org.dmyz.app. After the success of the creation in Identifiers-app IDs can be viewed, click on the creation of the App ID, in the expanded menu click the [Settings] button to enter the configuration interface.

In the configuration interface to find Push notifications, click [Create Certificate ...] button to create an SSL certificate. Development SSL Certificate and Production SSL certificate corresponding development environment and production environment, select Development SSL Certificate.

You can also add a APNs certificate by clicking on the certificatesof the left-hand navigation menu. Select Apple Push Notification service SSL (Sandbox), followed by the same procedure.

The first step is to be a CSR (certificate request) file, open utility in the Mac (this article is 10.8)-Keychain -Keychain Access-Certificate assistant-Request a certificate from a certification authority , fill in the Mailbox and name (DMYZ), and select [Save to disk]. Get a certificate request file called Certificatesigningrequest.certsigningrequest, and add two records to the login-key, a public key and a private key:

Back to the Apple Developer Center page, click [Continue] to continue and upload the generated certificatesigningrequest.certsigningrequest file. Complete this step to get the. cert file.
Provisioning Profiles

Running a program on an iOS device requires development provisioning profile, which is not directly linked to APNs and has been configured successfully on a real device to skip.

In the app development process to debug on the real equipment, need Development provisioning profile and Development certificate.

On the left navigation menu-Devices, add an iOS device. The UUID can be found in itunes. Then add an iOS App Development's certificate and Provisioning profile:

Finally get a certificate file, and a. mobileprovision suffix of the file, is the provisioning profile file. Install the certificate file and import the. mobileprovision file through Xcode into the device. General connection to ISO equipment Xcode will automatically eject, can also be in the Xocde menu help-xcode help-devices Modify Provisioning profile file. The following figure is the provisioning profile created at the Apple Developer Center:

To ensure that the value of the app ID and the Target properies-bundle identifier for the program is the same, the code Sign error:a valid Provisioning profile matching The application ' s Identifier ' Org.dmyz.app ' could not to be found. Generate PEM

Switch to the left navigation menu of certificates, find the previous generated APNs Development iOS Certificate, click the [Download] button to download, get aps_development.cer. After installation, the corresponding certificate is found in the keychain Access . Next you will export the certificate and private key separately. Select the Apple Development iOS push Services:org.dmyz.app right click and select [Export Apple Development iOS push Services:org.dmyz.app The file format is switched to. cer and exported to cert.cer. Click on the left ▶ expand it, you can see the name Dmyz, category for [private key] of this subkey, selected export to KEY.P12.

After exporting these two files, the processing is processed with OpenSSL: $ openssl x509-in cert.cer-inform der-outform pem-out
$ OpenSSL pkcs12-in key.p12-out key.pem-nodes

The import Passwordis required and is empty if the export is not set. I also found another way in so: $ OpenSSL pkcs12-clcerts-nokeys-out cert.pem-in CERT.P12
$ OpenSSL pkcs12-nocerts-out key.pem-in KEY.P12
$ cat Cert.pem Key.pem > Key.pem

The main difference is that key does not add nodes parameters, and finally the two files merged. You can pass the test. You need to enter the Pem pass phrase for at least 4 characters to execute the second command.

Now you get the Key.pem and Cert.pem files, whether the service is Nodejs, Python or ruby, this only requires two files. Coding

The notification count, click on the function of the notification execution and other details set and location notification the same, the difference is to specify device. In Xcode, add a function to the APPDELEGATE.M file that opens the project to get the device token:

-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {
    NSLog (@ "Devicetoken:%@", Devicetoken);
}


Finally, install the APN and copy the KEY.PEM and CERT.PEM to the project directory. Execute the following code to push notifications to the specified device, which is the effect of receiving a notification when the program is running in the background and the screen is locked:

var APN = require (' APN ');
 
var token = ' a788a70f ... 79ecc6145 '; 64-Length Device token
 
var options = {"Gateway": "Gateway.sandbox.push.apple.com"},
    apnconnection = new APN. Connection (options),
    device = new APN. Device (token), note
    = new APN. Notification ();
 
Note.expiry = Math.floor (Date.now ()/1000) +;
Note.badge = 3;
Note.alert = "Anime post \ Click to view updated 1 articles";
Note.payload = {' Messagefrom ': ' Caroline '};
 
Apnconnection.pushnotification (note, device);






from:http://dmyz.org/archives/472






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.