天天看點

Java背景向蘋果APNS推送文字,圖檔,視訊功能

務虛廢話,直接來代碼

方式一: 推送文字,百度Demo比較多,顯示以下代碼:

try
    {
        
        //從用戶端擷取的deviceToken,在此為了測試簡單,寫固定的一個測試裝置辨別。
        String deviceToken = "f117797e2b98f3ca82c12e376dc5753debbff94396c7f427b22b086f65f6da89";
        System.out.println("Push Start deviceToken:" + deviceToken);
        //定義消息模式
        PayLoad payLoad = new PayLoad();
        payLoad.addAlert("this is test!");
        payLoad.addBadge(1);//消息推送标記數,小紅圈中顯示的數字。
        payLoad.addSound("default");
        payLoad.addCustomDictionary("url", "https://img.icos8.cc/image/hhvy");
        //注冊deviceToken
        PushNotificationManager pushManager = PushNotificationManager.getInstance();
        pushManager.addDevice("iPhone", deviceToken);
        //連接配接APNS
        String host = "gateway.sandbox.push.apple.com"; //測試
        //String host = "gateway.push.apple.com";//生産
        int port = 2195;
//            String certificatePath = "D:/p12/**-cert.p12";//用戶端生成的用于JAVA背景連接配接APNS服務的*.p12檔案位置
        String certificatePath = "C:/tomcat/aps_development_key.p12";//用戶端生成的用于JAVA背景連接配接APNS服務的*.p12檔案位置
        String certificatePassword = "123123";//p12檔案密碼。
        pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
        //發送推送
        Device client = pushManager.getDevice("iPhone");
        System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +" ");
        pushManager.sendNotification(client, payLoad);
        //停止連接配接APNS
        pushManager.stopConnection();
        //删除deviceToken
        pushManager.removeDevice("iPhone");
        System.out.println("Push End");
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
           

方式二: 推送圖檔

差別于方式一: 重寫Payload方式,添加一些屬性:

private static final String APS = "aps";
    private Map<String, Object> params;
    private String alert;
    private Integer badge;
    private String sound = "";
//    private Integer contentAvailable;
    // 新添加
    private Integer mutableContent;
    
// 新添加    
    public Integer getMutableContent() {
        return mutableContent;
    }
    public void setMutableContent(Integer mutableContent) {
        this.mutableContent = mutableContent;
    }
    
    private String alertBody;
    private String alertActionLocKey;
    private String alertLocKey;
    private String[] alertLocArgs;
    private String alertLaunchImage;

           

細節點:  類似于蘋果規定格式要求的json串,就行

方式三: 推送 視訊:

與方式二一樣;

提供思路: Java 背景推送消息至蘋果APNS,蘋果收到資訊,推送至指定的deviceToken,也就是裝置,用戶端接收到消息,看其中是否有mutable-content,擷取其值字段值為1,則說明推送的消息内容含有圖檔,用戶端擷取指定url,下載下傳并展示到目前裝置的通知欄