start

package com.jynine.javapns;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties; import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.Devices;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.PushQueue; import org.apache.commons.lang.StringUtils;
import org.json.JSONException; public class IosPushUtil {
public static String keystore = null;
public static String password = null;
public static String host = null;
public static Boolean production = true;//true:production false: sandbox
public static final int numberOfThreads = 8;
static{
Properties propertie = new Properties();
InputStream inputStream; try {
inputStream = IosPushUtil.class.getClassLoader()
.getResourceAsStream("push.properties");
propertie.load(inputStream);
keystore = propertie.getProperty("certificatePath");
password = propertie.getProperty("certificatePassword","123456");
host = propertie.getProperty("host","gateway.push.apple.com");
production = Boolean.valueOf(propertie.getProperty("production", "true"));
inputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
//pushMsgNotification("hello!!!2", true, "iostoken");
// pushBadgeNotification(1, "iostoken");
String[] devs= new String[10000];
for (int i = 0; i < devs.length; i++) {
devs[i] = "iostoken";
}
List<Device> devices=Devices.asDevices(devs);
System.out.println(devices.size());
//pushPayLoadByThread(devices, "Hello 2222222", 1, null, null);
//pushPayloadDevicePairs(devices, "Hello 111111111", 1, null, null);
//pushPayloadDevicePairs(devices, "Hello +++", 1, null, null);
queue(devices,"Hello 2222222", 1, null, null);
}
/**
* 推送一个简单消息
* @param msg 消息
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushMsgNotification(String msg,Object devices) throws CommunicationException, KeystoreException{
Push.alert(msg, keystore, password, production, devices);
}
/**
* 推送一个标记
* @param badge 标记
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushBadgeNotification(int badge,Object devices) throws CommunicationException, KeystoreException{
Push.badge(badge, keystore, password, production, devices);
}
/**
* 推送一个语音
* @param sound 语音
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushSoundNotification(String sound,Object devices) throws CommunicationException, KeystoreException{
Push.sound(sound, keystore, password, production, devices);
}
/**
* 推送一个alert+badge+sound通知
* @param message 消息
* @param badge 标记
* @param sound 声音
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushCombinedNotification(String message,int badge,String sound,Object devices) throws CommunicationException, KeystoreException{
Push.combined(message, badge, sound, keystore, password, production, devices);
}
/**
* 通知Apple的杂志内容
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void contentAvailable(Object devices) throws CommunicationException, KeystoreException{
Push.contentAvailable(keystore, password, production, devices);
}
/**
* 推送有用的调试信息
* @param devices 设备
* @throws CommunicationException
* @throws KeystoreException
*/
public static void test(Object devices) throws CommunicationException, KeystoreException{
Push.test(keystore, password, production, devices);
}
/**
* 推送自定义负载
* @param devices
* @param msg
* @param badge
* @param sound
* @param map
* @throws JSONException
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushPayload(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
PushNotificationPayload payload = customPayload(msg, badge, sound, map);
Push.payload(payload, keystore, password, production, devices);
}
/**
* 用内置线程推送负载信息
* @param devices
* @param msg
* @param badge
* @param sound
* @param map
* @throws Exception
*/
public static void pushPayLoadByThread(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map) throws Exception{
PushNotificationPayload payload = customPayload(msg, badge, sound, map);
Push.payload(payload, keystore, password, production, numberOfThreads, devices);
}
/**
* 推送配对信息
* @param devices
* @param msg
* @param badge
* @param sound
* @param map
* @throws JSONException
* @throws CommunicationException
* @throws KeystoreException
*/
public static void pushPayloadDevicePairs(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
PayloadPerDevice perDevice = null;
for (int i = 0; i <devices.size(); i++) {
perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
payloadDevicePairs.add(perDevice);
}
Push.payloads(keystore, password, production, payloadDevicePairs);
}
/**
* 用线程推配对信息
* @param devices
* @param msg
* @param badge
* @param sound
* @param map
* @throws Exception
*/
public static void pushPayloadDevicePairsByThread(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws Exception{
List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
PayloadPerDevice perDevice = null;
for (int i = 0; i <devices.size(); i++) {
perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
payloadDevicePairs.add(perDevice);
}
Push.payloads(keystore, password, production,numberOfThreads, payloadDevicePairs);
}
/**
* 队列多线程推送
* @param devices
* @param msg
* @param badge
* @param sound
* @param map
* @throws KeystoreException
* @throws JSONException
*/
public static void queue(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws KeystoreException, JSONException{
PushQueue queue = Push.queue(keystore, password, production, numberOfThreads);
queue.start();
PayloadPerDevice perDevice = null;
for (int i = 0; i <devices.size(); i++) {
perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
queue.add(perDevice);
}
}
/**
* 自定义负载
* @param msg
* @param badge
* @param sound
* @param map 自定义字典
* @return
* @throws JSONException
*/
private static PushNotificationPayload customPayload(String msg,Integer badge,String sound,Map<String,String> map) throws JSONException{
PushNotificationPayload payload = PushNotificationPayload.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;
}
}

  

利用javapns对IOS进行推送的更多相关文章

  1. iOS远程推送原理及实现过程

    ➠更多技术干货请戳:听云博客 推送通知,是现在的应用必不可少的功能.那么在 iOS 中,我们是如何实现远程推送的呢?iOS 的远程推送原理又是什么呢?在做 iOS 远程推送时,我们会遇到各种各样的问题 ...

  2. (七十三)iOS本地推送通知的实现

    iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...

  3. 【转】iOS消息推送实现过程记录

    客户端代码:链接地址 服务器代码:链接地址  链接地址 这里记录下iOS消息推送实现的全过程 首先,申请秘钥. 之后进入链接地址开发者,当然你得有啊!!!!! 点击这里 如图: 下面实现创建推送证书( ...

  4. (转)OpenFire源码学习之十八:IOS离线推送

    转:http://blog.csdn.net/huwenfeng_2011/article/details/43458213 IOS离线推送 场景: 如果您有iOS端的APP,在会话聊天的时候,用户登 ...

  5. 使用PushSharp给iOS应用推送消息

    PushSharp是一个C#编写的服务端类库,用于推送消息到各种客户端,支持iOS(iPhone/iPad).Android.Windows Phone.Windows 8.Amazo.Blackbe ...

  6. IOS远程推送

    IOS远程推送 一.关于推送通知 推送通知,也被叫做远程通知,是在iOS 3.0以后被引入的功能.是当程序没有启动或不在前台运行时,告诉用户有新消息的一种途径,是从外部服务器发送到应用程序上的.一般说 ...

  7. iOS 消息推送(APNs) 傻瓜式教程

    也可以去我的简书页面查看这篇文章 首先: 1.做iOS消息推送需要真机测试 2.做iOS消息推送需要有付费的开发者账号 是否继续看帖? 先学习一下相关的知识吧! 因为中途可能会遇到一些问题,这篇文章或 ...

  8. (转载)iOS 极光推送SDK 集成指南

    iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...

  9. iOS极光推送,两次Bundleid不一致( 开发证书没有通过验证 是否重新上传证书)的解决方案

    极光在配置ios端推送时,需要上传p12证书,如果遇到如下图:: 证书上传未通过的原因一般有: 1.当前上传的p12证书密码输入有误: 2. 证书导出的时候展开了证书,把个人私钥导了出来,导证书的时候 ...

随机推荐

  1. 缓慢变化维 (Slowly Changing Dimension) 常见的三种类型及原型设计(转)

    开篇介绍 在从 OLTP 业务数据库向 DW 数据仓库抽取数据的过程中,特别是第一次导入之后的每一次增量抽取往往会遇到这样的问题:业务数据库中的一些数据发生了更改,到底要不要将这些变化也反映到数据仓库 ...

  2. Flink官网文档翻译

    http://ifeve.com/flink-quick-start/ http://vinoyang.com/2016/05/02/flink-concepts/ http://wuchong.me ...

  3. RHEL7禁用网卡命名规则

    [root@rhel7 ~]# vi /etc/sysconfig/grub GRUB_TIMEOUT= GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g ...

  4. 基于Echarts的股票K线图展示

    发布时间:2018-10-31   技术:javascript+html5+canvas   概述 基于echarts的股票K线图展示,只需引用单个插件,通过简单配置,导入数据,即可实现炫酷复杂的K线 ...

  5. data1是字符串?需要加上引号

    07-22 15:55:29.832: E/AndroidRuntime(23914): FATAL EXCEPTION: main 07-22 15:55:29.832: E/AndroidRunt ...

  6. 【centos6】安装redis + phpredis 以及 常用配置参数

    1.redis-server和redis-cli安装文章:http://www.cnblogs.com/skyessay/p/6429988.html 1.前置条件:查看是否安装gcc,命令:gcc ...

  7. java读取txt字符串挨个写入int数组

    int []num=new int[1001]; FileReader fr = new FileReader("1.txt"); BufferedReader br = new ...

  8. webshell文件下载器

    拥有webshell的服务器可以ping通主机A,这就表明webshell可以向主机A post一些数据. 在主机A上开启一个接受文件的http服务,在webshell中使用文件上传命令进行上传. 主 ...

  9. 基于Android的小巫新闻客户端开发系列教程

    <ignore_js_op> 141224c6n6x7wmu1aacap7.jpg (27.51 KB, 下载次数: 0) 下载附件  保存到相册 23 秒前 上传   <ignor ...

  10. java struts2入门学习---文件下载的二种方式

    一.关于文件下载: 文件下载的核心思想即是将文件从一个地方拷贝到另一个地方. 1.传统方式: 在Action中加入大量servlet api 操作.优点是好理解,缺点是耦合度高. 2.stream方式 ...