極光推送消息——RegistrationID方式

一、工具類android

package com.test.util;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.yjl.entity.pushMessage.JpushClient;

public class JpushClientUtil {

    private final static String appKey = "極光平臺建立應用的appKey";
    private final static String masterSecret = "極光平臺建立應用的masterSecret";
    private static JPushClient jPushClient = new JPushClient(masterSecret, appKey);

  public static void main(String[] args) {
    JpushClient jpushClient = new JpushClient();
    jpushClient.setRegistrationId("1a0018970afae35532f");
    jpushClient.setMsgContent("這個是消息內容");
    jpushClient.setMsgTitle("這個是標題");
    jpushClient.setNotificationTitle("這個是通知標題");
    jpushClient.setExtrasParam("這個是其餘參數內容");
    int result = JpushClientUtil.sendToRegistrationId(jpushClient);
    System.out.println("推送結果:"+result);
  }

  /**
   * 0推送失敗,1推送成功
   * @param jpushClient
   * @return
   */
    public static int sendToRegistrationId(JpushClient jpushClient) {
      int result = 0;
      try {
        PushPayload pushPayload = JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(jpushClient);
        System.out.println(pushPayload);
        PushResult pushResult = jPushClient.sendPush(pushPayload);
        System.out.println(pushResult);
        if (pushResult.getResponseCode() == 200) {
          result = 1;
        }
      } catch (APIConnectionException e) {
        e.printStackTrace();

      } catch (APIRequestException e) {
        e.printStackTrace();
      }

      return result;
    }

    private static PushPayload buildPushObject_all_registrationId_alertWithTitle(JpushClient jpushClient) {
      String registrationId = jpushClient.getRegistrationId();
      String notification_title = jpushClient.getNotificationTitle();
      String msg_title =  jpushClient.getMsgTitle();
      String msg_content = jpushClient.getMsgContent();
      String extrasparam = jpushClient.getExtrasParam();
      System.out.println("----------buildPushObject_all_all_alert");
      // 建立一個IosAlert對象,可指定APNs的alert、title等字段
      // IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title",
      // "alert body").build();
      return PushPayload.newBuilder()
        // 指定要推送的平臺,all表明當前應用配置了的全部平臺,也能夠傳android等具體平臺
        .setPlatform(Platform.all())
        // 指定推送的接收對象,all表明全部人,也能夠指定已經設置成功的tag或alias或該應應用客戶端調用接口獲取到的registration
        // id
        .setAudience(Audience.registrationId(registrationId))
        // jpush的通知,android的由jpush直接下發,iOS的由apns服務器下發,Winphone的由mpns下發
        .setNotification(Notification.newBuilder()
          // 指定當前推送的android通知
          .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification_title)
            .setTitle(notification_title)
            // 此字段爲透傳字段,不會顯示在通知欄。用戶能夠經過此字段來作一些定製需求,如特定的key傳要指定跳轉的頁面(value)
            .addExtra("sss", extrasparam).build())
          // 指定當前推送的iOS通知
          .addPlatformNotification(IosNotification.newBuilder()
            // 傳一個IosAlert對象,指定apns title、title、subtitle等
            .setAlert(notification_title)
            // 直接傳alert
            // 此項是指定此推送的badge自動加1
            .incrBadge(1)
            // 此字段的值default表示系統默認聲音;傳sound.caf表示此推送以項目裏面打包的sound.caf聲音來提醒,
            // 若是系統沒有此音頻則以系統默認聲音提醒;此字段若是傳空字符串,iOS9及以上的系統是無聲音提醒,如下的系統是默認聲音
            .setSound("sound.caf")
            // 此字段爲透傳字段,不會顯示在通知欄。用戶能夠經過此字段來作一些定製需求,如特定的key傳要指定跳轉的頁面(value)
            .addExtra("content", extrasparam).build())
          // 此項說明此推送是一個background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
          // 取消此註釋,消息推送時ios將沒法在鎖屏狀況接收
          // .setContentAvailable(true)
          .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(false)
          // 此字段是給開發者本身給推送編號,方便推送者分辨推送記錄
          .setSendno(1)
          // 此字段的值是用來指定本推送的離線保存時長,若是不傳此字段則默認保存一天,最多指定保留十天;
          .setTimeToLive(86400)

          .build())

        .build();

    }


}

二、對象ios

package com.test.entity.pushMessage;

public class JpushClient {

  private String registrationId;
  private String notificationTitle;
  private String msgTitle;
  private String msgContent;
  private String extrasParam;

  public String getRegistrationId() {
    return registrationId;
  }

  public void setRegistrationId(String registrationId) {
    this.registrationId = registrationId;
  }

  public String getNotificationTitle() {
    return notificationTitle;
  }

  public void setNotificationTitle(String notificationTitle) {
    this.notificationTitle = notificationTitle;
  }

  public String getMsgTitle() {
    return msgTitle;
  }

  public void setMsgTitle(String msgTitle) {
    this.msgTitle = msgTitle;
  }

  public String getMsgContent() {
    return msgContent;
  }

  public void setMsgContent(String msgContent) {
    this.msgContent = msgContent;
  }

  public String getExtrasParam() {
    return extrasParam;
  }

  public void setExtrasParam(String extrasParam) {
    this.extrasParam = extrasParam;
  }
}

參考連接: https://blog.csdn.net/LWJdear/article/details/77374415api

相關文章
相關標籤/搜索