使用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
        

你可能感兴趣的:(使用java代码实现推送IOS消息)