Remote notification push (with Java-side backend code)

Source: Internet
Author: User
Tags ssl certificate

IOS based on APNs message push principle and implementation (Java background)

Transferred from: http://ytwhw.iteye.com/blog/1744148

The principle of push:

The working mechanism of Push can be summarized simply as



In the figure, provider is the push server for an iphone software, which I will use. NET as the provider.

APNS is an abbreviation for Apple push Notification Service (Apple push server) and is Apple's server.

Can be divided into three stages.

First stage: The Push server application packages the message to be sent, the identity of the iphone, and sends it to APNS.

Phase II: APNs in its own list of iphone's registered push services, look for an iphone with a corresponding logo and send the message to the iphone.

The third stage: The iphone sends the message to the appropriate application and follows the settings to eject the push notification.



From what we can see.

1, the first is the application registration message push.

2, iOS and APNs server to Devicetoken. The application accepts Devicetoken.

3. The application sends the Devicetoken to the push service-side program.

4. The service-side program sends messages to the APNS service.

5. The APNs service sends the message to the iphone app.

Both the iphone client and APNs, provider and APNs need to be connected through a certificate. The following describes the production of the certificate used.

I. CSR documents

1. Generate Certificate Signing Request (CSR)



2, fill in your mailbox and common name, and choose to save to the hard disk.



Click Continue:



This generates a Pushtest.certsigningrequest file locally.

Second, SSL certificate file

1. Log in to the iOS Provisioning Portal with your paid account number and create a certificates (can be omitted), such as:









Click Submit

The creation of the certificate is complete.

2. Create a new app ID

Click on the new APP ID

Enter Description,bundle Identifier, click Submit, and the new app ID is complete.

Find the new app ID click on the Configure on the right:



Development push SSL Certificate differs from production push SSL Certificate in that one is a push certificate for development and one is a push certificate for publishing a product. Two certificates get to the terminal Devicetoken is not the same, with two certificate generated P12 certificate for Java Background Connection APNs server address is also different, development Push SSL Certificate The server address for the corresponding connection is: gateway.sandbox.push.apple.com. Production Push SSL Certificate The server address of the corresponding connection is: gateway.push.apple.com.

Click the development Push SSL Certificate line after the Configure:

Click Continue:




Select the previously generated pushtest.certsigningrequest file, click Generate, and the following page appears:



Click Continue:



Click Download to download the generated certificate for the supported push service (named: aps_development-6.cer).

Click Done and you will notice that the status becomes enabled:

So far, we have generated two files:

1, Pushtest.certsigningrequest

2. Aps_development-6.cer (Download the generated certificate that supports the push service.) )

Double-click Aps_development-6.cer to register in your keychain, so your keychain will have

Third, prepare the profile certificate, because the push message can only be tested on the real machine, so to build a profile certificate

Click "New Profile" to create a profile for the new app ID above and download pushtestdescdevprofile.mobileprovision after successful

Double-click to add it to Xcode's provisioning Profiles.

Iv. generate a Java backend certificate for connecting APNs:

Open Keychain


Select Apple development IOS Push Services:com.easecom.zhwgpushtestdesc and right-click to export it.

Export the P12 certificate for the Java background connection APNs.

Enter the password for the P12 certificate, which I used in this article is 123456. Remember this password, the Java background uses the P12 certificate to use.

Enter the password to access the keychain: System login password.

The Export PUSHTEST.P12 certificate is complete.

So far, we have generated four files:

1, Pushtest.certsigningrequest

2. Aps_development-6.cer (Download the generated certificate that supports the push service.) )

3, Pushtestdescdevprofile.mobileprovision
4, PUSHTEST.P12

This completes the IOS message push (Java background) certificate.

The following code begins:

Five, the iOS port code:

1, first in the project APPDELEGATE.M to add the following two agent methods

-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {
NSString *token = [NSString stringwithformat:@ "%@", Devicetoken];

To obtain the terminal equipment identification, this identity needs to be sent through the interface to the server side, server-side push message to APNs need to know the identity of the terminal, APNs through the registered terminal identity to find terminal equipment.
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 didfinishlaunchingwithoptions: (NSDictionary *) The Launchoptions method joins the registration message notification push ability; joins when the application is in an inactive state, determines whether it is triggered by a remote message notification, and joins the purge message push notification token.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{

Determines whether the application starts by a remote message notification trigger

if ([Launchoptions objectforkey:uiapplicationlaunchoptionsremotenotificationkey]!=nil) {

Gets the number of application message notification tokens (that is, the number in the small red circle)

int badge = [UIApplication sharedapplication].applicationiconbadgenumber;
if (badge>0) {

If the number of application message notification tokens (that is, the number in the small red circle) is greater than 0, clear the marker.

badge--;

Clears the tag. Clear the small red circle in the number, the small red circle in the number of 0, the small red circle will be eliminated.
[UIApplication sharedapplication].applicationiconbadgenumber = badge;
}
}

Message Push Registration
[[UIApplication Sharedapplication] registerforremotenotificationtypes:uiremotenotificationtypesound| Uiremotenotificationtypealert| Uiremotenotificationtypebadge];

}
3, add the message Receive processing agent method in the project APPDELEGATE.M.

Handling received message pushes
-(void) Application: (UIApplication *) application
Didreceiveremotenotification: (nsdictionary *) userInfo
{

The received message is processed here.
NSLog (@ "Receive Remote notification:%@", userInfo);
}
Vi. Java Backend Code:

public static void Main (string[] args) throws Exception
{
Try
{
The Devicetoken obtained from the client, in order to test the simple, write a fixed test device identity.
String Devicetoken = "Df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4"

System.out.println ("Push Start devicetoken:" + Devicetoken);
Defining Message Patterns
PayLoad PayLoad = new PayLoad ();
Payload.addalert ("This is test!");
Payload.addbadge (1);//The number of message push marks, the number shown in the small red circle.
Payload.addsound ("Default");
Register Devicetoken
Pushnotificationmanager Pushmanager = Pushnotificationmanager.getinstance ();
Pushmanager.adddevice ("IPhone", Devicetoken);
Connection APNs
String host = "gateway.sandbox.push.apple.com";
String host = "gateway.push.apple.com";
int port = 2195;

String Certificatepath = "C:/PUSHTEST.P12";//*.P12 file location generated earlier for the Java Background Connection APNs service
String Certificatepassword = "123456";//p12 file password.
Pushmanager.initializeconnection (host, Port, Certificatepath, Certificatepassword, sslconnectionhelper.keystore_ TYPE_PKCS12);
Send push
Device client = Pushmanager.getdevice ("IPhone");
SYSTEM.OUT.PRINTLN ("Push message:" + client.gettoken () + "\ n" +payload.tostring () + "");
Pushmanager.sendnotification (client, payLoad);
Stop Connection APNs
Pushmanager.stopconnection ();
Delete Devicetoken
Pushmanager.removedevice ("IPhone");
System.out.println ("Push End");
}
catch (Exception ex)
{
Ex.printstacktrace ();
}
}
}

The test passed.

Above in Iphone4,ipad2 device test pass, iphone3g,3gs need to play pushdoctor (push doctor) patch to test pass.

Remote notification push (with Java-side backend code)

Related Article

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.