javapns 推送ios消息

分類:編程 時間:2016-11-08
[摘要:Javapns 推收音訊進程:(我那邊是PC收收) 客戶端(即PC)收收一個要求 我們本身的辦事端吸收要求 辦事端經過apns,往蘋果辦事器收收音訊推收要求 蘋果辦事器支到要求,依據]

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>





Tags: version package public 服務器 import

文章來源:


ads
ads

相關文章
ads

相關文章

ad