Java pushes to iphone applications

Source: Internet
Author: User

According to the company's project requirements, we need to push messages to iphone applications. At the beginning, we chose php, but php language knowledge is a little understandable, Which is troublesome to develop, all messages are pushed in a familiar java language.

Jar packages to be dependent on:

 

 

The Code is as follows:

[Java]
/**
* MainSend. java
* Copyright (C) 2012
* Creation: cuiran 2012-07-24 11:31:35
*/
Package com. wpn. iphone. send;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import javapns. devices. Device;
Import javapns. devices. implementations. basic. BasicDevice;
Import javapns. notification. AppleNotificationServerBasicImpl;
Import javapns. notification. PushNotificationManager;
Import javapns. notification. PushNotificationPayload;
Import javapns. notification. PushedNotification;
 
Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;
 
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class MainSend {
Private static Log log = LogFactory. getLog (MainSend. class. getName ());

/*************************************** *********
Test push server address: gateway.sandbox.push.apple.com/2195
Product push server address: gateway.push.apple.com/2195
 
JavaPNS_2.2.jar is required.
 
**************************************** ***********/
/**
 
* This is a simple push method,
 
* Apple push Method
 
* @ Param tokens the token obtained by the iphone
 
* @ Param path: A file path in. p12 format. You need to apply for a file path on the apple website.
 
* @ Param password p12 password note that the exported certificate password cannot be blank because an error is reported when the password is empty.
 
* @ Param message refers to the content of the message pushed.
 
* @ Param count the value in the red circle on the application icon
 
* @ Param sendCount single-click or group-based true: single-click false: group-based
 
*/
 
Public void sendpush (List <String> tokens, String path, String password, String message, Integer count, boolean sendCount ){
 
Try {
// Message is a json string {"aps": {"alert": "iphone push test "}}
 
PushNotificationPayload payLoad = PushNotificationPayload. fromJSON (message );

PayLoad. addAlert ("iphone push test www.baidu.com"); // message content

PayLoad. addBadge (count); // the value in the red circle on the iphone app icon

PayLoad. addSound ("default"); // ringtone default


 
PushNotificationManager pushManager = new PushNotificationManager ();

// True: indicates the product release PUSH Service; false: indicates the product test push service.

PushManager. initializeConnection (new AppleNotificationServerBasicImpl (path, password, false ));

List <PushedNotification> notifications = new ArrayList <PushedNotification> ();

// Send a push message

If (sendCount ){

Log. debug ("-------------------------- apple push ticket -------");

Device device = new BasicDevice ();

Device. setToken (tokens. get (0 ));

PushedNotification notification = pushManager. sendNotification (device, payLoad, true );

Communications. add (notification );

} Else {

Log. debug ("-------------------------- apple push group -------");

List <Device> device = new ArrayList <Device> ();

For (String token: tokens ){

Device. add (new BasicDevice (token ));

}

Notifications = pushManager. sendconfigurications (payLoad, device );

}

List <PushedNotification> failedNotifications = PushedNotification. findFailedNotifications (notifications );

List <PushedNotification> successfulNotifications = PushedNotification. findSuccessfulNotifications (notifications );

Int failed = failedconfigurations. size ();

Int successful = successfulications ications. size ();



If (successful> 0 & failed = 0 ){

Log. debug ("----- All communications pushed successful (" + successfulications ications. size () + "):");

} Else if (successful = 0 & failed> 0 ){

Log. debug ("----- All events failed (" + failedevents. size () + "):");

} Else if (successful = 0 & failed = 0 ){

System. out. println ("No such notifications cocould be sent, probably because of a critical error ");

} Else {

Log. debug ("------ Some events failed (" + failedevents. size () + "):");

Log. debug ("------ Others succeeded (" + successfulNotifications. size () + "):");

}

// PushManager. stopConnection ();
 
} Catch (Exception e ){

E. printStackTrace ();

}
 
}

/**
* TODO
* @ Param args
*/
Public static void main (String [] args ){
MainSend send = new MainSend ();
List <String> tokens = new ArrayList <String> ();
Tokens. add ("success ");
Tokens. add ("dc2cf037bd4465c851b1d96a86b0a028307bc7e443435b6fafe93c2957bb213c ");
String path = "E :\\ iphone \ WPNPushService. p12 ";
String password = "wappin2009 ";
String message = "{'aps ': {'alert': 'iphone push test www.baidu.com '}}";
Integer count = 1;
Boolean sendCount = false;
Send. sendpush (tokens, path, password, message, count, sendCount );

}
 
}

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.