Jpush Aurora Push API Tool Class (JAVA) __java

Source: Internet
Author: User
Tags aliases error status code try catch
Package Com.util.push;
Import cn.jpush.api.common.resp.APIConnectionException;
Import cn.jpush.api.common.resp.APIRequestException;
Import Cn.jpush.api.push.model.PushPayload.Builder;
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.audience.AudienceTarget;
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 Org.apache.log4j.Logger;

Import Java.util.Map; /** * @author Supermudada * @ClassName: Messagepushutil * @Description: TODO (message Push tool Class) * TODO (review document Http://docs.jiguang.
  cn/jpush/server/push/rest_api_v3_push/) * @Created-DATE:2017/4/22 15:04 * * public class Messagepushutil {  private static final String Master_secret = "";     TODO (fill in your Master_secret) private static final String App_key = "";
    TODO (fill in your app_key) private static Logger Logger = Logger.getlogger (Jpushutils.class);
    private static Pushpayload pushpayload;

    private static Builder Builder =pushpayload.newbuilder (); public static void Main (string[] args) throws Exception {//todo (build push content, push target, push type)//pushpayload = message
        Pushutil.pushandroidandiosbyalias ("30", "haha", "Hehei");
        Pushpayload=pushpayload.alertall ("haha");
    TODO (start push) Sendpushtrycatch (pushpayload);
     /** * @param @param payload * @Title: Sendpushtrycatch TODO (Start push) * @Description: Try Catch push */public static void Sendpushtrycatch (Pushpayload payload) {jpushclient jpushclient = new Jpushclient (MASTER
        _secret, App_key);
            try {pushresult Pushresult = Jpushclient.sendpush (payload); Logger.info ("return result" + pushresult);
        catch (Apiconnectionexception e) {logger.error ("Connection error, try later" + E);
            catch (Apirequestexception e) {logger.error ("Aurora Push Internal Error", E);
            Logger.info ("Network request Status" + E.getstatus ());
            Logger.info ("Error status Code" + E.geterrorcode ());
            Logger.info ("error message" + E.geterrormessage ());
            Logger.info ("Information id" + e.getmsgid ());
        Logger.info ("Aurora push error message:" + e.geterrormessage ()); }/** * @param alias name * @param alert push title * @param content push (recommended JSON format) * @retu RN/public static pushpayload Pushandroidandiosbyalias (string alias, string alert, string content) {RE Turn builder. Setplatform (Platform.android_ios ())//push platform. Setaudience (Audience.alias (Alia s)//push target, here specifies to give an alias push. Setnotification (Notification.newbuilder (). Setalert (Alert
     ). Addplatformnotification (                           Androidnotification.newbuilder (). Addextra ("Sign", "5")
                        . Addextra ("content", content). Build () . Addplatformnotification (Iosnotification.newbuilder () Addextra ("Si
                        GN "," 5 "). Addextra (" content ", content). Build ())
                                . Build ()). SetOptions (Options.newbuilder () . Setapnsproduction (false)//ios push environment, True to push production, false indicates to push development environment; settimetolive (0)////Push the offline message for the user when the current user is not online so that it can be pushed again when it is online. The default is 86400 (1 days), and the maximum is 10 days.
    Setting to 0 means that offline messages are not preserved, and only users who push the current online can receive the. build ().  /** * @param @param alert * @param @return settings file * @return Pushpayload  return type * @throws * @Title: Buildpushobjectallallalert * @Description: TODO (Fast build Push object: All platforms, all devices, content for alert's pass 
        Know) */@SuppressWarnings ("static-access") public static Pushpayload Buildpushobjectallallalert (String alert) {
    return Pushpayload.alertall (alert);    /** * @param @param alert * @param @param alias * @param @return Setting file * @return Pushpayload
     return type * @throws * @Title: Buildpushobjectaliasalert * @Description: TODO (all platforms, push targets are aliases for alias, alert content is alert)
                */public static pushpayload Buildpushobjectaliasalert (string alert, string ... alias) {return builder . Setplatform (Platform.android_ios ()). Setaudience (Audience.alias (alias)). SE Tnotification (Notification.newbuilder (). Setalert (alert). addplatformnotif
                       Ication (Androidnotification.newbuilder ()                 . Addextra ("Sign", "5"). Build ()). Addplat
                                Formnotification (Iosnotification.newbuilder (). Addextra ("Sign", "5")
    . Build ()). Build ().
     /** * @param @param alias * @param @param alert * @param @param badge * @param settings file * @return pushpayload return type * @throws * @Title: Buildpushobjectios * @Description: TODO (iOS push badge
                                                        Sound) */public static pushpayload buildpushobjectiosandroid (map<string, string> params,
        String[] Alias, string alert, int badge, string sound, string msgcontent) {
                Return builder. Setplatform (Platform.android_ios ()). Setaudience (Audience.alias (alias))
   . Setnotification (Notification.newbuilder ()                     . Addplatformnotification (Iosnotification.newbuilder (). Setalert (Alert
                                ). Setbadge (Badge). Addextras (params) . Setsound (sound). Build (). Addplatformnotification (A Droidnotification.newbuilder (). Setalert (alert). Addextras (params). Build ()). Build ()). Setmessage (MESSAGE.N 
    Ewbuilder (). Setmsgcontent (msgcontent). Build ())-build (); /** * @param @param params * @param @param alias * @param @return settings file * @return Pushpay Load return type * @throws * @Title: Buildpushobjectallaliasalert * @Description: TODO (all platforms, push targets are aliases for alias, notification title
   To title, alert for alert)  */public static pushpayload Buildpushobjectallaliasalert (map<string, string> params, string alert, string tit Le, String ... alias) {return builder. Setplatform (Platform.android_ios ()). Setaud Ience (Audience.alias (alias)). Setnotification (Notification.newbuilder (). Setalert ( alert). Addplatformnotification (Androidnotification.newbuilder ().
                        Settitle (title). Addextras (params). Build ()
                                . Addplatformnotification (Iosnotification.newbuilder (). Addextras (params)
    . Build ()). Build ().
     /** * @param @param tag * @param @param alert * @param @param title * @param settings file * @return pushpayload return type * @throWS * @Title: Buildpushobjectandroidtagalertwithtitle * @Description: TODO (Platform is Android, the target is tag for tag device, content is Andr  OID alert alert, with title titled title)/public static pushpayload Buildpushobjectandroidtagalertwithtitle (string tag, string Alert, String title) {return builder. Setplatform (Platform.android ()). Setaudien 
    CE (Audience.tag (tag)). Setnotification (notification.android (alert, title, null)). build ();  /** * @param @param tag * @param @param tagall * @param @param number * @param @param  Alert * @param @param msgcontent * @param @return settings file * @return pushpayload return type * @throws *  @Title: Buildpushobjectiostagandalertwithextrasandmessage * @Description: TODO (Build Push object: Platform is IOS, push target is tag, Tagall intersection, The push content includes both the notification and the message-the notification message is alert, the angle is number, and the message content is msgcontent.
     The notification is the APNS push channel, and the message is the Jpush message channel in the application.
 * APNs push environment is "development" (if not explicitly set, the Library will be designated as development by default)    * True indicates push production environment, False indicates to push development environment *)/public static pushpayload Buildpushobjectiostagandalertwithextrasa
                Ndmessage (string tag, string tagall, int number, string alert, String msgcontent) {return builder . Setplatform (Platform.ios ()). Setaudience (Audience.tag_and (tag, tagall)). SE
                                Tnotification (Notification.newbuilder (). Addplatformnotification (Iosnotification.newbuilder ()
                                . Setalert (Alert). Setbadge (number)
                . Build ()). Build ()). Setmessage (Message.content (msgcontent))
                . SetOptions (Options.newbuilder (). Setapnsproduction (False). Build ())
    . build ();
    /** * Build Push object: Platform is andorid with IOS, * push Target is (TAG1 and TAG2), * Push content is-content for msgcontent message * * @param @param tag1 * @param @param tag2 * @param @param msgcontent * @param set file * @return RN pushpayload return type * @throws * @Title: Buildpushobjectiosaudiencemoremessagewithextras * @Description: TODO () */public static pushpayload Buildpushobjectiosaudiencemoremessagewithextras (String tag1, Str
                ing Tag2, String msgcontent) {return builder. Setplatform (Platform.android_ios ())
                        . Setaudience (Audience.newbuilder (). Addaudiencetarget (Audiencetarget.tag (TAG1, Tag2)) 
                        . Build ()). Setmessage (Message.newbuilder (). Setmsgcontent (Msgcontent)
    . Build ()).
 }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.