ios java 程序_使用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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值