javapns 推送ios消息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moyanxuan_1993_2_24/article/details/51691712

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

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

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

<dependency>
    <groupId>com.github.fernandospr</groupId>
    <artifactId>javapns-jdk16</artifactId>
    <version>2.3.1</version>
</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<String> tokens = new ArrayList<String>();
            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<PushedNotification> notifications = new ArrayList<PushedNotification>();
            // 开始推送消息
            if (sendCount) {
                Device device = new BasicDevice();
                device.setToken(deviceToken);
                PushedNotification notification = pushManager.sendNotification(
                        device, payload, true);
                notifications.add(notification);
            } else {
                List<Device> devices = new ArrayList<Device>();
                for (String token : tokens) {
                    devices.add(new BasicDevice(token));
                }
                notifications = pushManager.sendNotifications(payload, devices);
            }

            List<PushedNotification> failedNotification = PushedNotification
                    .findFailedNotifications(notifications);
            List<PushedNotification> 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>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.11</version>
</dependency>




猜你喜欢

转载自blog.csdn.net/moyanxuan_1993_2_24/article/details/51691712