直接上Demo

import java.util.ArrayList;
import java.util.List; import org.apache.commons.lang3.StringUtils; 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; public class PushPNS { public static void main(String[] args) throws Exception { // 设备的 Token 值
String deviceToken = "4f9e701ce3cb47173e3b3da5cdeb677297157b6be616689677e8e13c9b9ae652";
// push的内容
String alert = "我的push测试";
// 图标小红圈的数值
int badge = 1;
// 铃音
String sound = "default"; List<String> tokens = new ArrayList<String>();
tokens.add(deviceToken); // 推送证书的路径
String certificatePath = "/Users/ivy/Desktop/Duke/Eclipse/JinanLine/WebContent/acer.p12";
// 证书的密码
String certificatePassword = "123456";// 此处注意导出的证书密码不能为空因为空密码会报错
boolean sendCount = true; try { PushNotificationPayload payLoad = new PushNotificationPayload();
payLoad.addAlert(alert); // 消息内容
payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值、 if (!StringUtils.isBlank(sound)) {
payLoad.addSound(sound); // 铃音
}
PushNotificationManager pushManager = new PushNotificationManager();
// true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
pushManager
.initializeConnection(new AppleNotificationServerBasicImpl(
certificatePath, certificatePassword, false));
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
// 发送push消息
if (sendCount) { 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();
int successful = successfulNotifications.size(); pushManager.stopConnection();
}
catch (Exception e) { e.printStackTrace();
}
}
}
  • 需要导入的包

bcprov-jdk15on-151.jar
commons-lang3-3.1.jar
JavaPNS_2.2.jar
log4j-1.2.17.jar

  • demo链接

http://files.cnblogs.com/files/duke-cui/Apns.zip

IOS 基于APNS消息推(JAVA后台)的更多相关文章

  1. IOS 基于APNS消息推送原理与实现(JAVA后台)

    Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Pu ...

  2. 转:IOS 基于APNS消息推送原理与实现(JAVA后台)

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  3. IOS 基于APNS消息推送原理与实现(JAVA后台)--转

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  4. iOS 基于APNS消息推送原理与实现(包括JAVA后台代码)

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  5. 基于APNs最新HTTP/2接口实现iOS的高性能消息推送(服务端篇)

    1.前言 本文要分享的消息推送指的是当iOS端APP被关闭或者处于后台时,还能收到消息/信息/指令的能力. 这种在APP处于后台或关闭情况下的消息推送能力,通常在以下场景下非常有用: 1)IM即时通讯 ...

  6. 基于C++ 苹果apns消息推送实现(2)

    1.该模块的用途C++ 和 Openssl 代码 它实现了一个简单的apns顾客 2.配套文件:基于boost 的苹果apns消息推送实现(1) 3.最初使用的sslv23/sslv2/sslv3仅仅 ...

  7. oc学习之路----APNS消息推送从证书到代码(2015年4月26号亲试可用)

    前言:看这篇博客之前要准备:首先的有一个99刀的个人开发者账号或者199刀的企业开发者账号,其次你用的是apns消息推送,(本人之前四处打听有没有其他消息推送的方法:收获如下:首先如果想做到apns的 ...

  8. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  9. Push:iOS基于APNS的消息推送

    1. Push的三个步骤,如下图所示: (1)Push服务应用程序把要发送的消息.目的iPhone的标识打包,发给APNS: (2)APNS在自身的已注册Push服务的iPhone列表中,查找有相应标 ...

随机推荐

  1. python的列表与shell的数组

    python:names=["a","b","c"] shell:names=(a b c)

  2. 深入浅出HashMap

    /** *@ author ViVi *@date 2014-6-11 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.希望通过仪器讨论.共同提高~ 1 ...

  3. 更新python的依赖包,亲测!

    输入pip install --upgrade pandas 无需卸载,让它自己更新就好 pandas可以改成别的包

  4. 使用自编译的Emacs26.0.50build10版本,helm报错(已解决)

    使用自编译的Emacs26.0.50build10版本,helm报错(已解决) */--> code {color: #FF0000} pre.src {background-color: #0 ...

  5. 金额格式化,例子:fmoney("12345.675910", 3),返回12,345.676

    /** * 金额格式化 * 例子:fmoney("12345.675910", 3),返回12,345.676 * @data 备注lhh 2016-09-18 */ functi ...

  6. 51-python基础-python3-列表-常用列表方法- index()方法

    index()方法 1-可以传入一个值,如果该值存在于列表中,就返回它的下标. 实例1: 2-如果该值不在列表中,Python 就报 ValueError. 实例2: 3-如果列表中存在重复的值,就返 ...

  7. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

  8. 从FreeBSD里面看到的网络协议列表,感觉可以保存一下

    # # Internet protocols # # $FreeBSD$ # from: @(#)protocols 5.1 (Berkeley) 4/17/89 # # See also http: ...

  9. go中加号的用法

    // + 的用法 package main import "fmt" func main() { // 加号两端都是数字的话,做加法运算 var i = 1 var j = 1 v ...

  10. LeetCode Linked List Easy 21. Merge Two Sorted Lists

    Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...