JAVA 基于APNS实现ios推送

maven坐标

        
            com.github.fernandospr
            javapns-jdk16
            2.4.0
        

代码

import javapns.Push;
import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.*;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @ClassName: IosPush
 * @Author: 半仙
 * @Date: 2019/7/31 17:09
 * @Description :
 */
@Slf4j
public class IosPush2 {
    public static void main(String[] args) throws Exception {
        
        //war包项目跟路径
        //String keystore = Thread.currentThread().getContextClassLoader().getResources("fileName").getPath();
        String keystore = "";//证书,p12后缀
        String password = "";//证书密码
        String deviceToken = ""; //设备token, 需要去空
        PushNotificationPayload payload = new PushNotificationPayload();
        payload.addBadge(1);//图标小红圈的数值
       // payload.addAlert("alert");
        payload.addCustomAlertBody("fuck me");//与上行 二留一
        //自定义k,v
        ///payload.addCustomDictionary("key","val");
        //payload.addCustomDictionary("key2","val2");
        payload.addSound("default");//铃声
        //发送单条  
        //PushedNotifications result = Push.payload(payload, keystore, password, false, devicesToken);

        List devices = new LinkedList<>();
        devices.add(deviceToken);
        //多线程群发  返回结果为集合对象  false 为指定开发环境
        PushedNotifications result = Push.payload(payload, keystore, password, false , 1, devices);
        //jar包输入流,  重载支持,filePath,byte[],InputStream
       // PushedNotifications result = Push.payload(payload, resource.getInputStream(), password, false , 2, iosTokens);
        //失败集合
        PushedNotifications failedNotifications = result.getFailedNotifications();
        System.out.println("失败数量:"+failedNotifications.size());
        failedNotifications.stream().map(fail->fail.getDevice()).collect(Collectors.toList()).forEach(token-> log.info("失败token:"+token));
        //成功集合
        PushedNotifications successfulNotifications = result.getSuccessfulNotifications();
        System.out.println("成功数量:"+successfulNotifications.size());
}

你可能感兴趣的:(JAVA 基于APNS实现ios推送)