javapns 推送ios消息

javapns 推送消息过程:(我这边是PC发送)

  1. 客户端(即PC)发送一个请求
  2. 我们自己的服务端接收请求
  3. 服务端通过apns,往苹果服务器发送消息推送请求
  4. 苹果服务器收到请求,根据设备devicetoken,然后往设备发送消息,并返回结果给我们自己的服务端
  5. 我们服务端返回结果给客户端

需要jar包,我这有现成的
javapns jar 下载
jar包maven地址:

<dependency>
    <groupId>com.github.fernandosprgroupId>
    <artifactId>javapns-jdk16artifactId>
    <version>2.3.1version>
dependency>

下面请看代码

package pushmessage;

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 = "12345678";//导出证书时设置的密码
            String deviceToken = "6846f471b699868036547869977935cbf98df3805ecb2c1b7e7140b4fc962c94"; //手机设备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 = "F:/mdm.p12";
            boolean sendCount = true;

            PushNotificationPayload payload = new PushNotificationPayload();
            payload.addAlert(message); // 消息内容
            payload.addBadge(badge);


            //payload.addCustomAlertBody(msgEX);
            if (null == sound || "".equals(sound)) {
                payload.addSound(sound);
            }

            PushNotificationManager pushManager = new PushNotificationManager();
            // true:表示的是产品测试推送服务 false:表示的是产品发布推送服务
            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
                    certificatePath, msgCertificatePassword, true));
            List notifications = new ArrayList();
            // 开始推送消息
            if (sendCount) {
                Device device = new BasicDevice();
                device.setToken(deviceToken);
                PushedNotification notification = pushManager.sendNotification(
                        device, payload, true);
                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==========消息推送完毕");
    }
}

直接运行main函数就ok,测试通过。

注意:该jar包依赖于 log4j.jar 所以你的项目里还应该有log4j的jar包,不然会报错,log4j jar 包不要错了。

log4j maven地址:

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.11version>
dependency>




你可能感兴趣的:(【java】,Java)