Apple PNS Push and wake up

Source: Internet
Author: User

Using Apple's own push server

Certificatepath Push Certificate

Voipcertificatepath Wakeup Certificate

Certificatepassword Certificate Password

The above three items are required to use the App Store listed on the project to apply for a certificate, certificate application steps to find Niang

Required Packages

Maven

<!--apple Push pack--
<dependency>
<groupId>com.github.fernandospr</groupId>
<artifactId>javapns-jdk16</artifactId>
<version>2.4.0</version>
</dependency>

private static String certificatepath= "E:/apache-tomcat-7.0.37/webapps/estateservice/iosapp push certificate. P12"; iOS Developer certificate path, certificate with publish certificate and test certificate
private static String voipcertificatepath= "E:/apache-tomcat-7.0.37/webapps/estateservice/voip push certificate. P12"; iOS Wake-up certificate
private static String certificatepassword= "123456"; Certificate Password
private static Boolean production=false; Represents a product release push service false: Indicates a product test push service
private static String voipappleurl= "gateway.sandbox.push.apple.com"; Wake-Up server address test server path: Gateway.push.apple.com Release product server path: gateway.sandbox.push.apple.com
private static int port=2195; Wake-Up server address IP


/**
* Push a simple message
* @param msg
* @param devices
* @throws communicationexception
* @throws keystoreexception
*/
public void Pushmsgnotification (String msg,list<string> tokens) throws Communicationexception, keystoreexception{
list<device> devices = new arraylist<device> ();
for (String Token:tokens) {
try {
Devices.add (new Basicdevice (token));
} catch (Invaliddevicetokenformatexception e) {
E.printstacktrace ();
}
}
Push back result set
list<pushednotification> notifications = new arraylist<pushednotification> ();
notifications= Push.alert (msg, Certificatepath, Certificatepassword, production, devices);
Get failed result set
list<pushednotification> failednotifications = pushednotification.findfailednotifications (notifications);
Get a successful result set
list<pushednotification> successfulnotifications = pushednotification.findsuccessfulnotifications ( notifications);
int failed = Failednotifications.size (); Number of push failures
int successful = Successfulnotifications.size (); Number of push successes
System.err.println ("tokens:" +tokens);
SYSTEM.ERR.PRINTLN ("failure:" +failednotifications);
SYSTEM.ERR.PRINTLN ("Apple Push failed:" +failed+ ");
SYSTEM.ERR.PRINTLN ("Apple pushes success:" +successful+ ");
}

/**
* Push a ALERT+BADGE+SOUND notification or device wake-up push
* @param tokens IOS Tokens Collection
* @param MSG Push message
* @param badge icon Small red circle value
* @param sound Ring
* @param isvoip 1:true Wakeup push 2:false message push
*/
public void Iospush (list<string> tokens,string msg,integer badge,string sound,boolean Isvoip) {
Try
{
Pushnotificationbigpayload payLoad = new Pushnotificationbigpayload ();
Payload.addalert (msg); Message content
Payload.addbadge (badge); The number on the small red circle on the iphone app icon
Payload.addsound (Stringutils.defaultifempty (sound, "Default");//Ring tone
Pushnotificationmanager Pushmanager = new Pushnotificationmanager ();
True: Indicates that the product publishing push service is false: indicates a product test push service
if (ISVOIP) {
Pushmanager.initializeconnection (New Applenotificationserverbasicimpl (Voipcertificatepath, CertificatePassword, CONNECTIONTOAPPLESERVER.KEYSTORE_TYPE_PKCS12, Voipappleurl, Port));
}else{
Pushmanager.initializeconnection (New Applenotificationserverbasicimpl (Certificatepath, CertificatePassword, Production));
}
list<pushednotification> notifications = new arraylist<pushednotification> ();
Send a push message
if (Tokens.size () ==1&&tokens.size () >0) {
Device device = new Basicdevice ();
Device.settoken (tokens.get (0));
Pushednotification notification = pushmanager.sendnotification (device, payLoad, true);
NOTIFICATIONS.ADD (notification);
}else{
list<device> Device = new arraylist<device> ();
for (String Token:tokens) {
Device.add (new Basicdevice (token));
}
notifications = Pushmanager.sendnotifications (payLoad, device);
}
list<pushednotification> failednotifications = pushednotification.findfailednotifications (notifications);
list<pushednotification> successfulnotifications = pushednotification.findsuccessfulnotifications ( notifications);
int failed = Failednotifications.size (); Number of push failures
int successful = Successfulnotifications.size (); Number of push successes
System.err.println ("tokens:" +tokens);
SYSTEM.ERR.PRINTLN ("failure:" +failednotifications);
SYSTEM.ERR.PRINTLN ("Apple Push failed:" +failed+ ");
SYSTEM.ERR.PRINTLN ("Apple pushes success:" +successful+ ");
Pushmanager.stopconnection ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}

/**
* Push Custom load
* @param Tokens Collection
* @param MSG Push message
* @param badge icon Small red circle value
* @param Sound
* @param map Extension message
* @throws jsonexception
* @throws communicationexception
* @throws keystoreexception
*/
public void Pushpayload (list<string> tokens, String msg,integer badge,string sound,map<string,string> Map) Throws Jsonexception, Communicationexception, keystoreexception{
Pushnotificationbigpayload payload = custompayload (msg, badge, sound, map);
list<device> devices = new arraylist<device> ();
for (String Token:tokens) {
try {
Devices.add (new Basicdevice (token));
} catch (Invaliddevicetokenformatexception e) {
E.printstacktrace ();
}
}
list<pushednotification> notifications = new arraylist<pushednotification> ();
notifications= push.payload (payload, Certificatepath, Certificatepassword, production, devices);
list<pushednotification> failednotifications = pushednotification.findfailednotifications (notifications);
list<pushednotification> successfulnotifications = pushednotification.findsuccessfulnotifications ( notifications);
int failed = Failednotifications.size (); Number of push failures
int successful = Successfulnotifications.size (); Number of push successes
System.err.println ("tokens:" +tokens);
SYSTEM.ERR.PRINTLN ("failure:" +failednotifications);
SYSTEM.ERR.PRINTLN ("Apple Push failed:" +failed+ ");
SYSTEM.ERR.PRINTLN ("Apple pushes success:" +successful+ ");
}
/**
* Push Custom wake-up load
* @param Tokens Collection
* @param MSG Push message
* @param badge icon Small red circle value
* @param Sound
* @param map Extension message
* @throws jsonexception
* @throws communicationexception
* @throws keystoreexception
*/
public void Voippushpayload (list<string> tokens, String msg,integer badge,string sound,map<string,string> Map) throws Jsonexception, Communicationexception, keystoreexception{
try {
Encapsulating expansion Messages
Pushnotificationbigpayload payload = custompayload (msg, badge, sound, map);
Create Devices collection storage, device
list<device> devices = new arraylist<device> ();
for (String Token:tokens) {
Devices.add (new Basicdevice (token));
}
iOS push
Pushnotificationmanager Pushmanager = new Pushnotificationmanager ();
Push mode
Pushmanager.initializeconnection (New Applenotificationserverbasicimpl (Voipcertificatepath, CertificatePassword, CONNECTIONTOAPPLESERVER.KEYSTORE_TYPE_PKCS12, Voipappleurl, Port));
Store push put back object collection
list<pushednotification> notifications = new arraylist<pushednotification> ();
Send a push message
notifications = pushmanager.sendnotifications (payload, devices);
list<pushednotification> failednotifications = pushednotification.findfailednotifications (notifications);
list<pushednotification> successfulnotifications = pushednotification.findsuccessfulnotifications ( notifications);
int failed = Failednotifications.size (); Number of push failures
int successful = Successfulnotifications.size (); Number of push successes
System.err.println ("tokens:" +tokens);
SYSTEM.ERR.PRINTLN ("failure:" +failednotifications);
SYSTEM.ERR.PRINTLN ("Apple Push failed:" +failed+ ");
SYSTEM.ERR.PRINTLN ("Apple pushes success:" +successful+ ");
} catch (Invaliddevicetokenformatexception e) {
E.printstacktrace ();
}
}

/**
* Custom Load
* @param msg
* @param badge
* @param Sound
* @param map Custom dictionary
* @return
* @throws jsonexception
*/
Private Pushnotificationbigpayload custompayload (String msg,integer badge,string sound,map<string,string> Map) Throws jsonexception{
Pushnotificationbigpayload payload = Pushnotificationbigpayload.complex ();
if (Stringutils.isnotempty (msg)) {
Payload.addalert (msg);
}
if (badge! = null) {
Payload.addbadge (badge);
}
Payload.addsound (Stringutils.defaultifempty (sound, "Default"));
if (Map!=null &&!map.isempty ()) {
Object[] keys = Map.keyset (). ToArray ();
object[] Vals = Map.values (). ToArray ();
if (keys!= null && vals! = null && keys.length = = vals.length) {
for (int i = 0; i < map.size (); i++) {
Payload.addcustomdictionary (String.valueof (Keys[i]), string.valueof (Vals[i]));
}
}
}
return payload;
}

Apple PNS Push and wake up

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.