JAVA Implementation of iPhone push Notification Server

Source: Internet
Author: User

Prerequisites,

After writing push notification to get device token and get device Token, you need to send the token string to the application.
Service
Provider.




If the device token cannot be obtained from the jailbreaking mobile phone, you can install pushdoctor through cydia. You can Google the installation method here. My jailbreaking mobile phone can get the token by installing the push patch.

The provider sends the token number, notification content, and notification form (for example, whether a prompt is displayed ).
Window, voice, etc.) sent to Apple's server (apns ).

The simplest provider implementation is to establish a secure connection (TSL or SSL) with the Apple Server through a certificate, and send data streams that meet Apple's requirements to the Apple Server after a connection is established through authentication.

Obtain certificate

Apple provides two access methods:

Developer for testing
Production, used for products

For internal testing, use developer.

Download the certificate through the IOS provisioning Portal:

This requirement:

Apple
The developer program account must be the highest-level agent (for enterprise accounts, it doesn't matter if it is a personal account). The agent account is the founding account; otherwise, the configure link cannot be seen;
The developer and product must be enabled after the configure operation.
Go to the configure link and click Download:

Process Certificate

If you are writing an objc program running on a Mac, skip this step without processing the certificate.

If it is used in Java
, The private key used for the certificate and the certificate supporting the notification mentioned above (note that it is not an iPhone
Developer certificate.

Generate a certificate:

When you click storage, a file Password is prompted:

Of course, the password can be blank.

The following message is displayed:

Enter the password of the Mac logon user.

File generation.

Write an instance for sending a notification

For MAC code writing, there is a ready-made project available: http://stefan.hafeneger.name/download/PushMeBabySource.zip

To import to xcode, you only need:

Set devicetoken to the token string of the device. In addition, change pathforresource:

Aps_pai_identity

In addition, copy the certificate downloaded in the obtained certificate step to the xcode project resources directory:


We can see that the file name is consistent with the above pathforresource parameter.

Then run the program to receive the push notification on the device.

Third-party dependency packages (downloaded below ):


Bcprov-jdk16-145-1.jar

Commons-io-2.0.1.jar

Commons-lang-2.5.jar

Log4j-1.2.16.jar

Javapns-jdk16-163.jar

Java code:

Package com. sdunisi. iPhone. apns. Send;

Import java. util. hashmap;
Import java. util. iterator;

Import javapns. Back. pushnotificationmanager;
Import javapns. Back. sslconnectionhelper;
Import javapns. Data. device;
Import javapns. Data. payload;

Public class mainapnssend {

Public static void main (string [] ARGs) throws exception {
Try {
String devicetoken = "e775b5892f3334427c14def8aa4d8189a4ec1c795020072f4baa7ee92e50b1db"; // the token obtained by the iPhone

Payload payload = new payload ();
Payload. addalert ("My push test"); // push content
Payload. addbadge (1); // The Value of the small red circle of the icon
Payload. addsound ("default"); // ringtone

Pushnotificationmanager pushmanager = pushnotificationmanager. getinstance ();
Pushmanager. adddevice ("iPhone", devicetoken );

// Connect to apns
/*************************************** *********
Test server address: gateway.sandbox.push.apple.com/port 2195
Product push server address: gateway.push.apple.com/2195
**************************************** ***********/
String host = "gateway.sandbox.push.apple.com ";
Int Port = 2195;
String certificatepath = "/users/jcjc/desktop/push_p.p12"; // export the certificate
String certificatepassword = "sunlg"; // note that the exported certificate password cannot be blank. An error is returned if the exported certificate password is empty.
Pushmanager. initializeconnection (host, port,
Certificatepath, certificatepassword,
Sslconnectionhelper. keystore_type_pkcs12 );

// Send push
Device client = pushmanager. getdevice ("iPhone ");
Pushmanager. sendnotification (client, payload );
Pushmanager. stopconnection ();

Pushmanager. removedevice ("iPhone ");
}
Catch (exception e ){
E. printstacktrace ();
}

}
}


Development Kit. Zip
(2384 K)

Project code apns_iphone.zip
(2348 K)

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.