Java實現激光推送android平臺

pom.xml裏

     <dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>
 
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jiguang-common</artifactId>
    <version>1.0.3</version>
</dependency>

JiPushUtil裏

/**
 * 發送給所有安卓用戶
 * @param notification_title 通知內容標題
 * @param msg_title 消息內容標題
 * @param msg_content 消息內容
 * @param extrasparam 擴展字段
 * @return 0推送失敗,1推送成功
 */
public int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
    int result = 0;
    try {
        PushPayload pushPayload= buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
        LOGGER.info(""+pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        LOGGER.info(""+pushResult);
        System.out.println(pushResult+"Android");
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

    return result;
}

 

/**
 * 向android平臺所有用戶推送消息
 * @param notification_title
 * @param msg_title
 * @param msg_content
 * @param extrasparam
 * @return
 */
private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
    LOGGER.info("----------向android平臺所有用戶推送消息中......");
    return PushPayload.newBuilder()
            //指定要推送的平臺,all代表當前應用配置了的所有平臺,也可以傳android等具體平臺
            .setPlatform(Platform.android())
            //指定推送的接收對象,all代表所有人,也可以指定已經設置成功的tag或alias或該應應用客戶端調用接口獲取到的registration id
            .setAudience(Audience.all())
            //jpush的通知,android的由jpush直接下發,iOS的由apns服務器下發,Winphone的由mpns下發
            .setNotification(Notification.newBuilder()
                    //指定當前推送的android通知
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .setAlert(notification_title)
                            .setTitle(notification_title)
                            //此字段爲透傳字段,不會顯示在通知欄。用戶可以通過此字段來做一些定製需求,如特定的key傳要指定跳轉的頁面(value)
                            .addExtra("androidNotification extras key",extrasparam)
                            .build())
                    .build()
            )
            //Platform指定了哪些平臺就會像指定平臺中符合推送條件的設備進行推送。 jpush的自定義消息,
            // sdk默認不做任何處理,不會有通知提示。建議看文檔http://docs.jpush.io/guideline/faq/的
            // [通知與自定義消息有什麼區別?]瞭解通知和自定義消息的區別
            .setMessage(Message.newBuilder()
                    .setMsgContent(msg_content)
                    .setTitle(msg_title)
                    .addExtra("message extras key",extrasparam)
                    .build())

            .setOptions(Options.newBuilder()
                    //此字段的值是用來指定本推送要推送的apns環境,false表示開發,true表示生產;對android和自定義消息無意義
                    .setApnsProduction(ApnsProduction)
                    //此字段是給開發者自己給推送編號,方便推送者分辨推送記錄
                    .setSendno(1)
                    //此字段的值是用來指定本推送的離線保存時長,如果不傳此字段則默認保存一天,最多指定保留十天,單位爲秒
                    .setTimeToLive(86400)
                    .build())
            .build();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章