JavaPns向APNS发送推送信息

最近开始搞iOS推送服务,使用Javapns构筑业务服务器。

Javapns可以从谷歌Code上下载到,目前最新版本号为2.2。

服务器环境的构筑略去不提,以下只记录下推送部分的代码,备忘。

①发送简单推送信息

最常见的推送形式,包括信息,提示音,过期时间等,客户端接受到推送后,会在提示中心看到推送内容和接收时间

List<PushSendDto> pushSendDtoList = new ArrayList<PushSendDto>();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList<Device>();
Device tempDevice = new BasicDevice();
tempDevice.setToken("your device token");  // 推送对象DeviceToken(开发模式与产品模式的DeviceToken不同)
tempDto.deviceList.add(tempDevice);
tempDto.payLoad = new PushNotificationPayload();
tempDto.payLoad.addAlert(ios push: Hello World!");
tempDto.payLoad.addSound("default");
Calendar cal = Calendar.getInstance();
cal.setTime(DateUtils.getNow());
int expiry = (int) (cal.getTimeInMillis() / 1000L) + 1200; // 推送信息过期时间 20分钟
tempDto.payLoad.setExpiry(expiry);
tempDto.payLoad.badge(10);
pushSendDtoList.add(tempDto);

// 推送开始
for (PushSendDto sendDto : pushDtoList) {
    List<PushedNotification> notifications = Push.payload(sendDto.payLoad, keystore, password, production, threadCount, sendDto.deviceList);
    result.successPushList.addAll(PushedNotification.findSuccessfulNotifications(notifications));  // 推送成功列表
    result.failurePushList.addAll(PushedNotification.findFailedNotifications(notifications));           // 推送失败列表
}


②只发送badge数字

只推送数字,收到推送时不会有提示音,同时提示中心也看不到推送内容

List<PushSendDto> pushSendDtoList = new ArrayList<PushSendDto>();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList<Device>();
Device tempDevice = new BasicDevice();
tempDevice.setToken("your device token");  // 推送对象DeviceToken(开发模式与产品模式的DeviceToken不同)
tempDto.deviceList.add(tempDevice);
tempDto.payLoad = new PushNotificationPayload();
tempDto.payLoad.badge(10);
pushSendDtoList.add(tempDto);

// 推送开始
for (PushSendDto sendDto : pushDtoList) {
    List<PushedNotification> notifications = Push.payload(sendDto.payLoad, keystore, password, production, threadCount, sendDto.deviceList);
    result.successPushList.addAll(PushedNotification.findSuccessfulNotifications(notifications));  // 推送成功列表
    result.failurePushList.addAll(PushedNotification.findFailedNotifications(notifications));           // 推送失败列表
}

③自定义发送

可以在标准发送信息以外自定义特殊的内容。

iOS系统在设定-->通知菜单里可以设定通知提示的方式,当应用没有运行或处于后台状态时,推送到来时提示方式可以设置为弹出对话框。

弹出对话框时可以部分定制的,其右边的按钮可以显示开发者需要的标题。

List<PushSendDto> pushSendDtoList = new ArrayList<PushSendDto>();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList<Device>();
Device tempDevice = new BasicDevice();
tempDevice.setToken("your device token");  // 推送对象DeviceToken(开发模式与产品模式的DeviceToken不同)
tempDto.deviceList.add(tempDevice);
tempDto.payLoad = new PushNotificationPayload();
tempDto.payLoad.addSound("default");
tempDto.payLoad.addCustomAlertActionLocKey("customized button title");  // 自定义按钮标题
tempDto.payLoad.addCustomAlertBody("Hello World!");
tempDto.payLoad.addCustomDictionary("otherCode", "12345678");
Calendar cal = Calendar.getInstance();
cal.setTime(DateUtils.getNow());
int expiry = (int) (cal.getTimeInMillis() / 1000L) + 120;
tempDto.payLoad.setExpiry(expiry);
pushSendDtoList.add(tempDto);

// 推送开始
for (PushSendDto sendDto : pushDtoList) {
    List<PushedNotification> notifications = Push.payload(sendDto.payLoad, keystore, password, production, threadCount, sendDto.deviceList);
    result.successPushList.addAll(PushedNotification.findSuccessfulNotifications(notifications));  // 推送成功列表
    result.failurePushList.addAll(PushedNotification.findFailedNotifications(notifications));           // 推送失败列表
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值