1. 程式人生 > >app推送-極光推送

app推送-極光推送

會不會有小夥伴一臉懵逼,問我什麼是App推送?

先甩一個熟悉的介面。這些就是推送。

file

這些推送有什麼作用呢,我百度百科了一下好處

做過app開發的都應該接觸到app推送。現在安卓推送 app只要是後臺程序被殺死使用者是是接受不到的。ios推送是沒有問題的,因為ios只有一個廠家使用的系統 而安卓雖然也是隻是一個系統但是但是被很多廠商修改了底層。這個時候如果你的老闆給你一個需求讓你做app推送,保證app推送使用者百分百接收到,那麼你就可以辭職了。你和安卓一塊辭職。是不是想想都可怕。原來我們公司是這麼做的,當app被鎖屏的時候留一個一畫素的點,保證app隨時存活,但是現在不可以了 只要清理了,app就會被殺死。

file

這個時候小夥伴就有人問我了,這個就沒辦法解決了麼???????????

file

現在可以整合各大廠商都自己研發的推送方式,比如小米推送、華為推送。他們自己推送肯定在自己的手機能收到,還有就是隻要你有錢什麼都能解決。。。。。

file

  哈哈哈哈 對的 你只要給各大廠商錢就可以開啟白名單通道,如果你是安卓你就可以看到,即使你把某些app程序給殺死了但是呢,依然還有煩人的推送,據聽說騰訊是超牛逼的,這話怎麼講,騰訊微信是肯定在白名單的不用想。但是呢,人家有自己開發的推送方式,只要騰訊有一個app是活著的,他就能把所有的騰訊產品拉起來,(這個時候小夥伴會問我什麼是拉起來,我解釋一下拉起來的含義就是能讓某些app自己啟動)是不是不可思議哈哈哈哈。今天我們要說的是極光推送,只要全部app都是極光推送,你們的app就是活著的,But極光是靠這個收費的,具體怎麼收費我給你們甩一個官網。 

點我進極光官網--小程式使用者直接百度極光推送

給大家說一個好訊息 工信部已經聯合了國內多家廠商要做統一推送服務,如果那一天真的到來那麼對於廣大國內開發者將會受益巨大。官網網址:http://chinaupa.com/ 

file

  我給大家的解決方案可以參考一下:
  目前解決推送問題通用的解決辦法是採用混合推送機制,對於有提供系統推送的廠商走廠商推送,其餘的走第三方                       推送。比如極光
系統級別的推送就是根據手機型號進行推送,判斷使用者手機型號進行分別進行sdk的切換。
對於沒有提供系統級推送的廠商那麼只能自己通過保活機制維持一個常駐後臺程序來實現。不過廠商為了系統純淨對後臺程序做了嚴格限制,導致目前的程序保活機制面臨幾乎無解的問題。
最後我們一起期待工信部的推送儘快與大家見面。。

今天說一下app極光推送後端邏輯

老規矩先扔極光推送文件的地址。 點我快速進入極光推送文件地址 極光不僅能推送還能看到app多少日活,有的投資人不看你們app後臺的資料,就看極光友盟之類的第三方外掛的資料 file

我們從demo看起,這是手動推送的介面。很簡潔。 file 但是呢,我們身為高階程式設計師怎麼可能用這種方法。 file

好嘞這個沒有什麼流程就給請求極光就可以了。甩一個工具類。

pom檔案 如果是maven專案直接引入 如果是web專案需要從官網下載jar包

  <!-- 極光推送 -->
	  <dependency>
		  <groupId>cn.jpush.api</groupId>
		  <artifactId>jpush-client</artifactId>
		  <version>3.3.7</version>
	  </dependency>
	 

package cn.cnbuilder.util;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/** 
 * 說明:極光推送工具類
 * @version
 * KingYiFan
 */
@Controller
@RequestMapping(value="/jpush")
public class JPushClientUtil{
	//極光後臺管理系統申請
	private static final String appKey = "xxxxx";
	private static final String masterSecret = "xxxxx";
	public static final int MAX = Integer.MAX_VALUE;
	public static final int MIN = MAX / 2;
	 private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
	
	/**
	 * 儲存離線的時長。秒為單位。最多支援10天(864000秒)。 
	 * 0 表示該訊息不儲存離線。即:使用者線上馬上發出,當前不線上使用者將不會收到此訊息。
	 * 此引數不設定則表示預設,預設為儲存1天的離線訊息(86400秒)。
	 */
	private static long timeToLive = 60 * 60 * 24;
	public static void main(String[] args) {
		String msgTitle = "測試推送,你是KingYiFan,哈哈哈!!!";
		String msgContent = "看到資訊了麼,看到就推送成功了!";
		String userid="使用者別名";
		JPushClientUtil.sendToAll("最新文章",msgTitle, "WZ", userid);
	}
	/**
	 * 保持 sendNo 的唯一性是有必要的
	 */
	public static int getRandomSendNo() {
		return (int) (MIN + Math.random() * (MAX - MIN));
	}
    /**
     * 推送給裝置標識引數的使用者
     * @param registrationId 裝置標識
     * @param notification_title 通知內容標題
     * @param msg_title 訊息內容標題
     * @param msg_content 訊息內容
     * @param extrasparam 擴充套件欄位
     * @return 0推送失敗,1推送成功
     */
    public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam,String extrasparam2) {
        int result = 0;
        try {
            PushPayload pushPayload= JPushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam,extrasparam2);
            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;
    }
 
    /**
     * 傳送給所有安卓使用者
     * @param notification_title 通知內容標題
     * @param msg_title 訊息內容標題
     * @param msg_content 訊息內容
     * @param extrasparam 擴充套件欄位
     * @return 0推送失敗,1推送成功
     */
    public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= JPushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
            System.out.println(pushPayload);
            PushResult pushResult=jPushClient.sendPush(pushPayload);
            System.out.println(pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
 
            e.printStackTrace();
        }
 
         return result;
    }
 
    /**
     * 傳送給所有IOS使用者
     * @param notification_title 通知內容標題
     * @param msg_title 訊息內容標題
     * @param msg_content 訊息內容
     * @param extrasparam 擴充套件欄位
     * @return 0推送失敗,1推送成功
     */
    public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload= JPushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
            System.out.println(pushPayload);
            PushResult pushResult=jPushClient.sendPush(pushPayload);
            System.out.println(pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
 
            e.printStackTrace();
        }
 
         return result;
    }
 
    /**
     * 傳送給所有使用者
     * @param notification_title 通知內容標題
     * @param msg_title 訊息內容標題
     * @param msg_content 訊息內容
     * @param extrasparam 擴充套件欄位
     * @return 0推送失敗,1推送成功
     */
    public static int sendToAll( String title, String content,String extrasparam, String extrasparam2) {
        int result = 0;
        try {
            PushPayload pushPayload= JPushClientUtil.buildPushObject_android_and_ios(title,content,extrasparam,extrasparam2);
            System.out.println(pushPayload);
            PushResult pushResult=jPushClient.sendPush(pushPayload);
            System.out.println(pushResult);
            if(pushResult.getResponseCode()==200){
                result=1;
            }
        } catch (Exception e) {
 
            e.printStackTrace();
        }
 
        return result;
    }
 
 
 
    public static PushPayload buildPushObject_android_and_ios(String title, String content, String extrasparam, String extrasparam2) {
    	    IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody(title, null,content).build();
    		return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(title)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .setTitle(title)
                                //此欄位為透傳欄位,不會顯示在通知欄。使用者可以通過此欄位來做一些定製需求,如特定的key傳要指定跳轉的頁面(value)
                                .addExtra("type",extrasparam)
                                .addExtra("id",extrasparam2)
                                .build()
                        )
                        .addPlatformNotification(IosNotification.newBuilder()
                                //傳一個IosAlert物件,指定apns title、title、subtitle等
                                .setAlert(iosAlert)
                                //直接傳alert
                                //此項是指定此推送的badge自動加1
                                .incrBadge(1)
                                //此欄位的值default表示系統預設聲音;傳sound.caf表示此推送以專案裡面打包的sound.caf聲音來提醒,
                                // 如果系統沒有此音訊則以系統預設聲音提醒;此欄位如果傳空字串,iOS9及以上的系統是無聲音提醒,以下的系統是預設聲音
                                .setSound("sound.caf")
                                //此欄位為透傳欄位,不會顯示在通知欄。使用者可以通過此欄位來做一些定製需求,如特定的key傳要指定跳轉的頁面(value)
                                .addExtra("type",extrasparam)
                                .addExtra("id",extrasparam2)
                                //此項說明此推送是一個background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                // .setContentAvailable(true)
 
                                .build()
                        )
                        .build()
                )
                //Platform指定了哪些平臺就會像指定平臺中符合推送條件的裝置進行推送。 jpush的自定義訊息,
                // sdk預設不做任何處理,不會有通知提示。建議看文件http://docs.jpush.io/guideline/faq/的
                // [通知與自定義訊息有什麼區別?]瞭解通知和自定義訊息的區別
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .setTitle(title)
                        .addExtra("type",extrasparam)
                        .addExtra("id",extrasparam2)
                        .build())
 
                .setOptions(Options.newBuilder()
                        //此欄位的值是用來指定本推送要推送的apns環境,false表示開發,true表示生產;對android和自定義訊息無意義
                        .setApnsProduction(true)
                        //此欄位是給開發者自己給推送編號,方便推送者分辨推送記錄
                        .setSendno(getRandomSendNo())
                        //此欄位的值是用來指定本推送的離線儲存時長,如果不傳此欄位則預設儲存一天,最多指定保留十天,單位為秒
                        .setTimeToLive(timeToLive)
                        .build()
                )
                .build();
    }
 
    private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam,String extrasparam2) {
 
        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.alias(registrationId))
                //jpush的通知,android的由jpush直接下發,iOS的由apns伺服器下發,Winphone的由mpns下發
                .setNotification(Notification.newBuilder()
                        //指定當前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()
 
                                .setAlert(notification_title)
                                .setTitle(notification_title)
                                
                                //此欄位為透傳欄位,不會顯示在通知欄。使用者可以通過此欄位來做一些定製需求,如特定的key傳要指定跳轉的頁面(value)
                                .addExtra("type",extrasparam)
                                .addExtra("id",extrasparam2)
                                .build())
                        //指定當前推送的iOS通知
                        .addPlatformNotification(IosNotification.newBuilder()
                                //傳一個IosAlert物件,指定apns title、title、subtitle等
                                .setAlert(msg_title)
                                //直接傳alert
                                //此項是指定此推送的badge自動加1
                                .incrBadge(1)
                                //此欄位的值default表示系統預設聲音;傳sound.caf表示此推送以專案裡面打包的sound.caf聲音來提醒,
                                // 如果系統沒有此音訊則以系統預設聲音提醒;此欄位如果傳空字串,iOS9及以上的系統是無聲音提醒,以下的系統是預設聲音
                                .setSound("default")
                                //此欄位為透傳欄位,不會顯示在通知欄。使用者可以通過此欄位來做一些定製需求,如特定的key傳要指定跳轉的頁面(value)
                                .addExtra("type",extrasparam)
                                .addExtra("id",extrasparam2)
                                //此項說明此推送是一個background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                //取消此註釋,訊息推送時ios將無法在鎖屏情況接收
                                // .setContentAvailable(true)
 
                                .build())
 
 
                        .build())
                //Platform指定了哪些平臺就會像指定平臺中符合推送條件的裝置進行推送。 jpush的自定義訊息,
                // sdk預設不做任何處理,不會有通知提示。建議看文件http://docs.jpush.io/guideline/faq/的
                // [通知與自定義訊息有什麼區別?]瞭解通知和自定義訊息的區別
                .setMessage(Message.newBuilder()
 
                        .setMsgContent(msg_content)
 
                        .setTitle(msg_title)
 
                        .addExtra("type",extrasparam)
                         .addExtra("id",extrasparam2)
 
                        .build())
 
                .setOptions(Options.newBuilder()
                        //此欄位的值是用來指定本推送要推送的apns環境,false表示開發,true表示生產;對android和自定義訊息無意義
                        .setApnsProduction(true)
                        //此欄位是給開發者自己給推送編號,方便推送者分辨推送記錄
                        .setSendno(getRandomSendNo())
                        //此欄位的值是用來指定本推送的離線儲存時長,如果不傳此欄位則預設儲存一天,最多指定保留十天;
                        .setTimeToLive(timeToLive)
 
                        .build())
 
                .build();
	    }
	 
	    private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
	        System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
	        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(true)
                        //此欄位是給開發者自己給推送編號,方便推送者分辨推送記錄
                        .setSendno(getRandomSendNo())
                        //此欄位的值是用來指定本推送的離線儲存時長,如果不傳此欄位則預設儲存一天,最多指定保留十天,單位為秒
                        .setTimeToLive(timeToLive)
                        .build())
                .build();
	    }
	 
	    private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
	        System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");
	        return PushPayload.newBuilder()
                //指定要推送的平臺,all代表當前應用配置了的所有平臺,也可以傳android等具體平臺
                .setPlatform(Platform.ios())
                //指定推送的接收物件,all代表所有人,也可以指定已經設定成功的tag或alias或該應應用客戶端呼叫介面獲取到的registration id
                .setAudience(Audience.all())
                //jpush的通知,android的由jpush直接下發,iOS的由apns伺服器下發,Winphone的由mpns下發
                .setNotification(Notification.newBuilder()
                        //指定當前推送的android通知
                        .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("iosNotification extras key",extrasparam)
                                //此項說明此推送是一個background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                               // .setContentAvailable(true)
 
                                .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(true)
                        //此欄位是給開發者自己給推送編號,方便推送者分辨推送記錄
                        .setSendno(getRandomSendNo())
                        //此欄位的值是用來指定本推送的離線儲存時長,如果不傳此欄位則預設儲存一天,最多指定保留十天,單位為秒
                        .setTimeToLive(timeToLive)
                        .build())
                .build();
	    }
}


這就是極光推送教程,哪裡不懂可以私信我哦!給大家留一個問題,推送你們是否有收到過帶聲音的推送。我最近在研究,歡迎一起學習。


鼓勵作者寫出更好的技術文件,就請我喝一瓶哇哈哈哈哈哈哈哈。。 微信:

支付寶:


感謝一路支援我的人。。。。。

Love me and hold me
QQ:69673804(16年老號)
EMAIL:[email protected]
友鏈交換
如果有興趣和本部落格交換友鏈的話,請按照下面的格式在評論區進行評論,我會盡快新增上你的連結。

網站名稱:KingYiFan’S Blog
網站地址:http://blog.cnbuilder.cn
網站描述:年少是你未醒的夢話,風華是燃燼的彼岸花。
網站Logo/頭像:[頭像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)