JavaPns向APNS发送推送信息

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

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

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

①发送简单推送信息

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

List pushSendDtoList = new ArrayList();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList();
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 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 pushSendDtoList = new ArrayList();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList();
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 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 pushSendDtoList = new ArrayList();
PushSendDto tempDto = new PushSendDto();
tempDto.deviceList = new ArrayList();
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 notifications = Push.payload(sendDto.payLoad, keystore, password, production, threadCount, sendDto.deviceList);
    result.successPushList.addAll(PushedNotification.findSuccessfulNotifications(notifications));  // 推送成功列表
    result.failurePushList.addAll(PushedNotification.findFailedNotifications(notifications));           // 推送失败列表
}


你可能感兴趣的:(iOS开发相关)