1. CSR File
1. Generate a Certificate Signing Request (CSR)
2. Enter your email address and common name, and save it to the hard disk.
Ii. SSL Certificate file
1. Add the test push certificate through the generated. certsigningrequest (the same is true for publishing the push Certificate)
2. Download and double-click to install
3. Open the key string-my certificate, right-click the certificate and choose export from the shortcut menu (If this option is not available, click other places and then right-click it), and create a password for the certificate.
Note: You can use this. p12 certificate in the Java background. The following steps are required for. pem certificates:
Apns certificate export PEM
OpenSSL X509-In aps_development.cer-inform der-out yourcertname. pem
Export apns certificate keys
Export the key under the apns certificate in the "key string" first.
Generate. p12 format.
Export PEM from p12.
Password required.
OpenSSL PKCS12-nocerts-out mobilecapkey. pem-In mobilecapkey. p12
You do not need to enter a password for export.
OpenSSL PKCS12-nodes-out mobilecapkey. pem-In mobilecapkey. p12
The parameters are different.
Merge Cer. PEM and key. pem
Cat yourcertname. pem mobilecapkey. pem> apns-dev.pem
V. IOS code:
1. First, add the following two proxy methods to the project's appdelegate. M:
-(Void) Application :( uiapplication *) Application didregisterforremotenotificationswithdevicetoken :( nsdata *) devicetoken {
Nsstring * token = [nsstring stringwithformat: @ "% @", [devicetoken
Substringwithrange: nsmakerange (1, tokenstr. Length-2)]
]; // Remove the beginning and end
// Obtain the terminal device ID. This ID must be sent to the server through the interface. When the server pushes a message to apns, it must know the terminal ID. apns finds the terminal device through the registered terminal ID.
Nslog (@ "my token is: % @", token );
}
-(Void) Application :( uiapplication *) Application didfailtoregisterforremotenotificationswitherror :( nserror *) error {
Nsstring * error_str = [nsstring stringwithformat: @ "% @", error];
Nslog (@ "failed to get token, error: % @", error_str );
}
2. In appdelegate. M (bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) The launchoptions method can be used to register Message notification push capabilities; when the application is not started, determines whether the message is triggered by a remote Message notification; adds the message push notification clearing tag.
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
// Determine whether the application is started by a remote Message notification trigger
If ([launchoptions objectforkey: uiapplicationlaunchoptionsremotenotificationkey]! = Nil ){
// Obtain the number of notification tags for the Application message (that is, the number in the red circle)
Int badge = [uiapplication sharedapplication]. applicationiconbadgenumber;
If (badge> 0 ){
// Clear the flag if the number of notification tags in the application message is greater than 0.
Badge --;
// Clear the flag. Clear the number in the red circle. If the number in the Red Circle is 0, the Red Circle is eliminated.
[Uiapplication sharedapplication]. applicationiconbadgenumber = badge;
}
}
// Message push Registration
[[Uiapplication sharedapplication] registerforremotenotificationtypes: uiremotenotificationtypesound | uiremotenotiftypetypealert | uiremotenotificationtypebadge];
}
-(Void) applicationdidbecomeactive :( uiapplication *) Application
{
// Click the notification to enter
// It is generally executed when the entire application is loaded. It will also be executed after it is suspended, so it is often used to clear the Red Circle
[Uiapplication sharedapplication]. applicationiconbadgenumber = 0;
}
3. Add the message receiving and processing proxy method to the project appdelegate. M.
// Process received message push
-(Void) Application :( uiapplication *) Application
Didreceiveremotenotification :( nsdictionary *) userinfo
{
// Process the received message here.
Nslog (@ "receive remote notification: % @", userinfo );
}
6. Java background code:
Public static void main (string [] ARGs) throws exception
{
Try
{
// Specify a fixed device identifier for the devicetoken obtained from the client.
String devicetoken = "df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 109d34ad 20175f8c4"
System. Out. println ("push start devicetoken:" + devicetoken );
// Define the message mode
Payload payload = new payload ();
Payload. addalert ("this is test! ");
Payload. addbadge (1); // Number of message push tags, which are displayed in a small red circle.
Payload. addsound ("default ");
// Register devicetoken
Pushnotificationmanager pushmanager = pushnotificationmanager. getinstance ();
Pushmanager. adddevice ("iPhone", devicetoken );
// Connect to apns
String host = "gateway.sandbox.push.apple.com ";
// String host = "gateway.push.apple.com ";
Int Port = 2195;
String certificatepath = "C:/pushtest. p12"; // The *. p12 file used to connect to the apns service in the Java background.
String certificatepassword = "123456"; // p12 File Password.
Pushmanager. initializeconnection (host, port, certificatepath, certificatepassword, sslconnectionhelper. keystore_type_pkcs12 );
// Send and push
Device client = pushmanager. getdevice ("iPhone ");
System. Out. println ("push message:" + client. gettoken () + "\ n" + payload. tostring () + "");
Pushmanager. sendnotification (client, payload );
// Stop connecting to apns
Pushmanager. stopconnection ();
// Delete devicetoken
Pushmanager. removedevice ("iPhone ");
System. Out. println ("Push end ");
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}
}
Note:
1. device token has nothing to do with the app. The obtained test certificate and publish certificate are different;
2. the token changes when the machine is flushed;
3. The certificate is valid for one year, and should be changed soon when it is about to expire. The replacement will not affect the operation of the old certificate;
4. format:
{
"APs ":
{
"Alert": "Message received ed from Bob ",
"Badge": 5,
"Sound": "bingbong. AIFF"
},
"Otherdata ":@""
}