app推送-極光推送

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

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

file

這些推送有什麼做用呢,我百度百科了一下好處程序員

作過app開發的都應該接觸到app推送。如今安卓推送 app只要是後臺進程被殺死用戶是是接受不到的。ios推送是沒有問題的,由於ios只有一個廠家使用的系統 而安卓雖然也是隻是一個系統可是可是被不少廠商修改了底層。這個時候若是你的老闆給你一個需求讓你作app推送,保證app推送用戶百分百接收到,那麼你就能夠辭職了。你和安卓一塊辭職。是否是想一想均可怕。原來咱們公司是這麼作的,當app被鎖屏的時候留一個一像素的點,保證app隨時存活,可是如今不能夠了 只要清理了,app就會被殺死。

file

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

file

如今能夠集成各大廠商都本身研發的推送方式,好比小米推送、華爲推送。他們本身推送確定在本身的手機能收到,還有就是隻要你有錢什麼都能解決。。。。。web

file

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

點我進極光官網--小程序用戶直接百度極光推送spring

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

file

我給你們的解決方案能夠參考一下:
  目前解決推送問題通用的解決辦法是採用混合推送機制,對於有提供系統推送的廠商走廠商推送,其他的走第三方                       推送。好比極光
系統級別的推送就是根據手機型號進行推送,判斷用戶手機型號進行分別進行sdk的切換。
對於沒有提供系統級推送的廠商那麼只能本身經過保活機制維持一個常駐後臺進程來實現。不過廠商爲了系統純淨對後臺進程作了嚴格限制,致使目前的進程保活機制面臨幾乎無解的問題。
最後咱們一塊兒期待工信部的推送儘快與你們見面。。

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

老規矩先扔極光推送文檔的地址。 點我快速進入極光推送文檔地址 極光不只能推送還能看到app多少日活,有的投資人不看大家app後臺的數據,就看極光友盟之類的第三方插件的數據 file後端

咱們從demo看起,這是手動推送的界面。很簡潔。 file 可是呢,咱們身爲高級程序員怎麼可能用這種方法。 fileapi

好嘞這個沒有什麼流程就給請求極光就能夠了。甩一個工具類。服務器

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:69673804@qq.com
友鏈交換
若是有興趣和本博客交換友鏈的話,請按照下面的格式在評論區進行評論,我會盡快添加上你的連接。

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