package com.notifation.server;
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 SendNotifation {
/**
* IPHONE推送
*
* @param tokens
* 设备ID列表
* @param message
* 推送的消息
* @param badge
* iphone应用图标上小红圈上的数值
* @param sound
* 消息提醒铃音
* @param certificatePath
* 证书路径
* @param certificatePassword
* 证书密码
* @param single
* true:单条 false:多条
*/
public void sendpush(List<String> tokens, String message, int badge,
String sound, String certificatePath, String certificatePassword,
boolean single) {
try {
PushNotificationPayload payLoad = new PushNotificationPayload();
if(message!=null && !message.equals("")){
payLoad.addAlert(message); // 消息内容
}
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 (single) {
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);
if (failedNotifications != null) {
int failed = failedNotifications.size();
String errorLog = "失败条数=" + failed + "; ";
for (PushedNotification failedNotification : failedNotifications) {
Device d = failedNotification.getDevice();
errorLog += "deviceId=" + d.getDeviceId() + "; token="
+ d.getToken();
}
System.out.println("消息推送失败记录:" + errorLog);
}
// int successful = successfulNotifications.size();
} catch (Exception e) {
System.out.println("消息推送异常..."+e.getMessage());
}
}
public static void main(String[] args) {
SendNotifation pushService = new SendNotifation();
List<String> token = new ArrayList<String>();
// String device_token = "759cfeed32a4d5241b09346af149b8a27cc204288c42c86c039232d0ccd526aa";
String device_token = "c138f426afe666b334bffef2142a32b38c201137e041fc233795349a383f108a";
token.add(device_token);
String certificatePath = "/Users/snjp/Desktop/Funever.p12";
String certificatePassword = "7592457";
pushService.sendpush(token, "hello,world", 1, "default", certificatePath,
certificatePassword, true);
}
}
wyl90116
- 粉丝: 7
- 资源: 25
最新资源
- (源码)基于Spring Boot框架的博客管理系统.zip
- (源码)基于ESP8266和Blynk的IR设备控制系统.zip
- (源码)基于Java和JSP的校园论坛系统.zip
- (源码)基于ROS Kinetic框架的AGV激光雷达导航与SLAM系统.zip
- (源码)基于PythonDjango框架的资产管理系统.zip
- (源码)基于计算机系统原理与Arduino技术的学习平台.zip
- (源码)基于SSM框架的大学消息通知系统服务端.zip
- (源码)基于Java Servlet的学生信息管理系统.zip
- (源码)基于Qt和AVR的FestosMechatronics系统终端.zip
- (源码)基于Java的DVD管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页