極光推送消息——RegistrationID方式


1、工具類

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();

    }


}

2、對象

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/77374415


免責聲明!

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



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