Java Implementation of iphone push notifications

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 server, that is, the 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 window is displayed or whether a voice is voiced) to the Apple 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:

 

The log-on apple 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 initial 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, You need to combine and export the private key used for the certificate and the certificate supporting the notification (note, not the iphone developer Certificate) mentioned above.

 

 

 

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 www.2cto.com

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 ();

}

 

}

}

 

From VV's RIA

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.