使用java代码实现推送IOS消息
新的工作中涉及到了给APP端推送消息,初次接触这方面知识,找了个Demo 记录一下
import java.util.ArrayList;
import java.util.List;
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 PushMsg {
public static void main(String[] args) throws Exception{System.out.println("zsl==========开始推送消息");
int badge = 1;
// 图标小红圈的数值
String sound = "default";
// 铃音
String msgCertificatePassword = "password";
//导出证书时设置的密码
String deviceToken = "your_mobile_token";
//手机设备token号
String message = "test push message to ios device";
List tokens = new ArrayList();
tokens.add(deviceToken);
//String certificatePath = requestRealPath
//+ "/WEB-INF/classes/certificate/msg.p12";
//java必须要用导出p12文件php的话是pem文件
String certificatePath = "C:\\444.p12";
boolean sendCount = true;
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(message);
// 消息内容
payload.addBadge(badge);
payload.addCustomDictionary("uid", "haahi");
payload.addCustomDictionary("type", 12);
payload.addCustomDictionary("title", "haahi");
payload.addSound("default.caf");
// 铃音PushNotificationManager pushManager = new PushNotificationManager();
// true:表示的是产品测试推送服务 false:表示的是产品发布推送服务
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
certificatePath, msgCertificatePassword, false));
List notifications = new ArrayList();
// 开始推送消息
if (sendCount) {
Device device = new BasicDevice();
device.setToken(deviceToken);
PushedNotification notification = pushManager.sendNotification(
device, payload, false);
notifications.add(notification);
} else {
List devices = new ArrayList();
for (String token : tokens) {
devices.add(new BasicDevice(token));
}
notifications = pushManager.sendNotifications(payload, devices);
}List failedNotification = PushedNotification
.findFailedNotifications(notifications);
List【使用java代码实现推送IOS消息】 successfulNotification = PushedNotification
.findSuccessfulNotifications(notifications);
int failed = failedNotification.size();
int successful = successfulNotification.size();
System.out.println("zsl==========成功数:" + successful);
System.out.println("zsl==========失败数:" + failed);
pushManager.stopConnection();
System.out.println("zsl==========消息推送完毕");
}
}
pom
com.github.fernandospr
javapns-jdk16
2.2.1
推荐阅读
- 10道不得不会的JavaEE面试题
- 校园生活助手小程序
- Tool|【前端】解决Bootstrap3与Bootstrap4同时使用的CSS冲突问题
- [开发杂谈记录] Windows CMD使用socket代理
- 使用Shifu在OpenYurt集群中接入RTSP协议摄像头
- Linux,Centos系统下配置java|Linux,Centos系统下配置java Jdk(附下载地址)
- [自制操作系统]|[自制操作系统] 第12回 实现中断代码
- 理解 JavaScript 中的内存管理(Memory Management)
- 微信小程序中使用canvas 卡顿问题
- java|基于SSM框架学习之Mybatis(一)详细步骤 附图