iOS 手機App消息推送功能(后台Java實現)


以下是一個測試demo

public static void main(String[] args){

    String deviceToken = "********************";//綁定的手機的token
    String alert = "您有新的消息";//push的內容 
    int badge =1;//圖標小紅圈的數值
    String sound = "default";//鈴音

    List<String> tokens = new ArrayList<String>();
    tokens.add(deviceToken);
    String certificatePath = "此處為證書.p12";
    String certificatePassword = "此處為證書密碼";//此處注意導出的證書密碼不能為空因為空密碼會報錯
    boolean sendCount = true;

    try {
        PushNotificationPayload payLoad = new PushNotificationPayload();
        payLoad.addAlert(alert); // 消息內容
        payLoad.addBadge(badge); // iphone應用圖標上小紅圈上的數值

        if (!StringUtils.isBlank(sound)) {
            payLoad.addSound(sound);//鈴音
        }
        PushNotificationManager pushManager = new PushNotificationManager();
        //true:表示的是產品發布推送服務 false:表示的是產品測試推送服務
        pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));
        List<PushedNotification> notifications = new ArrayList<PushedNotification>();
        // 發送push消息
        if (sendCount) {
            Device device = new BasicDevice();
            device.setToken(tokens.get(0));
            PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
            notifications.add(notification);
        } else {
            List<Device> device = new ArrayList<Device>();
            for (String token : tokens) {
                device.add(new BasicDevice(token));
            }
            notifications = pushManager.sendNotifications(payLoad, device);
        }
        pushManager.stopConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM