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();
    }
}
相關文章
相關標籤/搜索