Android推送之官方推送

隨著Android系統(tǒng)不斷升級衣式,應用后臺存活時間一直受限制,所以通過應用進程的NotificationManager彈出通知的方式并不可靠骡澈。

但是應用不存活罚勾,也要彈出通知的需求一直在珍策。Android不像IOS系統(tǒng)托启,IOS支持apns推送服務,所以天然是可靠的膛壹。為了解決這個問題驾中,各個手機廠商推出了自己的官方推送。

推送分為兩種:

  1. 透傳推送模聋,依賴應用進程的存活
  2. 官方推送肩民,由廠商推送,不依賴應用進程的存活

本文以 HUAWEI MI VIVO OPPO 四個手機廠商為例链方,分別貼出相應的接入須知和實現(xiàn)代碼持痰。

每個廠商都要由公司的運營或者專門的同學幫我們申請,當然祟蚀,如果公司規(guī)模較小工窍,那么只能由開發(fā)自己去申請。其他三家還好前酿,OPPO的官方推送申請是有條件患雏,其中有一條是即時通訊的APP不能接入。(大家接入時罢维,要仔細閱讀文檔)

HUAWEI

https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_sdkdownload_agent

按照如上文檔淹仑,引入華為的sdk。

AndroidManifest

 <!-- 接入HMSSDK PUSH模塊需要注冊肺孵,第三方相關(guān) :接收Push消息(注冊匀借、Push消息、Push連接狀態(tài))廣播平窘,
                此receiver類需要開發(fā)者自己創(chuàng)建并繼承com.huawei.hms.support.api.push.PushReceiver類吓肋,
                參考示例代碼中的類:com.huawei.hmsagent.HuaweiPushRevicer
            Access to the HMSSDK push module requires registration:
                Receive push message (registration, push message, push connection state) broadcast.
                This receiver class requires the developer to create and inherit the com.huawei.hms.support.api.push.PushReceiver class.
                Reference to class in sample code: Com.huawei.hmsagent.HuaweiPushRevicer-->
        <receiver android:name=".push.HuaWeiPushReceiver">
            <intent-filter>
                <!-- 必須,用于接收token | Must, for receiving token -->
                <action android:name="com.huawei.android.push.intent.REGISTRATION"/>
                <!-- 必須瑰艘,用于接收消息 | Must是鬼, used to receive messages-->
                <action android:name="com.huawei.android.push.intent.RECEIVE"/>
                <!-- 可選肤舞,用于點擊通知欄或通知欄上的按鈕后觸發(fā)onEvent回調(diào) | Optional, click the button on the notification bar or the notification bar to trigger the onevent callback -->
                <action android:name="com.huawei.android.push.intent.CLICK"/>
                <!-- 可選,查看push通道是否連接屑咳,不查看則不需要 | Optional, query whether the push channel is connected or not -->
                <action android:name="com.huawei.intent.action.PUSH_STATE"/>
            </intent-filter>
        </receiver>

HuaWeiPushReceiver

  1. 用來記錄華為推送的token萨赁,以便于告知自己的服務
  2. 接受透傳推送
public class HuaWeiPushReceiver extends PushReceiver {

    private static final String TAG = "HuaWeiPushReceiver";

    public static String getToken() {
        return tokenStore.get();
    }

    private static final PreferenceStore.StringStore tokenStore = PreferenceStore.ofString("sun.push.huawei.token", "");

    @Override
    public void onToken(Context context, String token) {
        super.onToken(context, token);

        if (PushInit.PUSH_LOG) {
            Log.i(TAG, "onToken() called with: context = [" + context + "], token = [" + token + "]");
        }
        Logger.i(true, "華為推送 token: %s", token);

        if (!TextUtils.isEmpty(token)) {
            tokenStore.set(token);
        }
        PushRegisterProxy.registerHuaWeiPush();
    }

    @Override
    public boolean onPushMsg(Context context, byte[] msgBytes, Bundle extras) {
        if (PushInit.PUSH_LOG) {
            Log.i(TAG, "onPushMsg() called with: context = [" + context + "], msgBytes = [" + new String(msgBytes, Chars.UTF_8) + "], extras = [" + extras + "]");
        }
        final String msg = new String(msgBytes, Chars.UTF_8);
        PassThroughPushCenter.onReceivePush(msg, PassThroughPushCenter.TYPE_HUAWEI);
        return true;
    }

    @Override
    public void onEvent(Context context, Event event, Bundle extras) {
        super.onEvent(context, event, extras);
        if (PushInit.PUSH_LOG) {
            Log.i(TAG, "onEvent() called with: context = [" + context + "], event = [" + event + "], extras = [" + extras + "]");
        }

    }

    @Override
    public void onPushState(Context context, boolean pushState) {
        super.onPushState(context, pushState);
        if (PushInit.PUSH_LOG) {
            Log.i(TAG, "onPushState() called with: context = [" + context + "], pushState = [" + pushState + "]");
        }
    }
}

HuaWeiPushDispatcher
當點擊華為的官方推送,會顯示此Activity兆龙,點擊后的操作,都由HuaWeiPushDispatcher觸發(fā)敲董。

public class HuaWeiPushDispatcher extends Activity {

    private static final String TAG = "HuaWeiPushDispatcher";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        final Intent intent = getIntent();
        final String data = intent.getStringExtra("data");


        final BasePushMsgEntity model = Jsons.parseJson(data, BasePushMsgEntity.class);
        Log.i(TAG, "onCreate: " + data);
        if (model == null || !model.isValid()) {
            finish();
            return;
        }

        NotifyPushCenter.onNotifyPush(this, data, model, NotifyPushCenter.NotifyPushChannel.HwPush);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
    }
}

很簡單紫皇,我們通過intent獲取到推送過來的數(shù)據(jù),然后處理腋寨。最后將此Activity關(guān)掉即可

小米

AndroidManifest

<receiver
            android:name=".push.MiPushReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE"/>
            </intent-filter>
            <intent-filter>
                <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED"/>
            </intent-filter>
            <intent-filter>
                <action android:name="com.xiaomi.mipush.ERROR"/>
            </intent-filter>
        </receiver>

MiPushReceiver實現(xiàn)PushMessageReceiver聪铺,在onNotificationMessageClicked處理點擊事件即可

public class MiPushReceiver extends PushMessageReceiver {

    private static final String TAG = "MiPushReceiver";


    public static final String APP_ID = "*****";
    public static final String APP_KEY = "*****";


    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {

        if (DEBUG) {
            Logger.d("接收到消息推送透傳消息 message: %s", message);
        }
        PassThroughPushCenter.onReceivePush(message.getContent(), PassThroughPushCenter.TYPE_MI_PUSH);
    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageArrived(context, miPushMessage);
        Log.d(TAG, "onNotificationMessageArrived() called with: context = [" + context + "], miPushMessage = [" + miPushMessage + "]");
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageClicked(context, miPushMessage);

        NotifyPushCenter.onNotifyPush(context, miPushMessage.getContent(), NotifyPushCenter.NotifyPushChannel.MiPush);
    }

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String regId = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                if (DEBUG) {
                    Logger.d("小米推送回調(diào) regId:%s", regId);
                }
                PushRegisterProxy.registerMiPush();
            }
        }
    }
}

OPPO

暫缺

VIVO

AndroidManifest

<receiver android:name="com.iksocial.queen.push.VivoPushMessageReceiver">
            <intent-filter>
                <!--接受push消息-->
                <action android:name="com.vivo.pushclient.action.RECEIVE"/>
            </intent-filter>
        </receiver>

VivoPushMessageReceiver ,同樣的萄窜,onNotificationMessageClicked點擊事件處理即可铃剔。

public class VivoPushMessageReceiver extends OpenClientPushMessageReceiver {
    @Override
    public void onNotificationMessageClicked(Context context, UPSNotificationMessage message) {
        NotifyPushCenter.onNotifyPush(context, message.getSkipContent(), NotifyPushCenter.NotifyPushChannel.VPush);
    }

    @Override
    public void onReceiveRegId(Context context, String s) {
        Log.i(TAG, "onReceiveRegIdvivopush: " + s);
        PushRegisterProxy.registerVivoPush();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市查刻,隨后出現(xiàn)的幾起案子键兜,更是在濱河造成了極大的恐慌,老刑警劉巖穗泵,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件普气,死亡現(xiàn)場離奇詭異,居然都是意外死亡佃延,警方通過查閱死者的電腦和手機现诀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來履肃,“玉大人仔沿,你說我怎么就攤上這事〕咂澹” “怎么了封锉?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長陡鹃。 經(jīng)常有香客問我烘浦,道長,這世上最難降的妖魔是什么萍鲸? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任闷叉,我火速辦了婚禮,結(jié)果婚禮上脊阴,老公的妹妹穿的比我還像新娘握侧。我一直安慰自己蚯瞧,他們只是感情好,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布品擎。 她就那樣靜靜地躺著埋合,像睡著了一般。 火紅的嫁衣襯著肌膚如雪萄传。 梳的紋絲不亂的頭發(fā)上甚颂,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音秀菱,去河邊找鬼振诬。 笑死,一個胖子當著我的面吹牛衍菱,可吹牛的內(nèi)容都是我干的赶么。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼脊串,長吁一口氣:“原來是場噩夢啊……” “哼辫呻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起琼锋,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤放闺,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后斩例,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雄人,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年念赶,在試婚紗的時候發(fā)現(xiàn)自己被綠了础钠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡叉谜,死狀恐怖旗吁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情停局,我是刑警寧澤很钓,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站董栽,受9級特大地震影響码倦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜锭碳,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一袁稽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧擒抛,春花似錦推汽、人聲如沸补疑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽莲组。三九已至,卻和暖如春暖夭,著一層夾襖步出監(jiān)牢的瞬間锹杈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工迈着, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嬉橙,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓寥假,卻偏偏與公主長得像,于是被迫代替她去往敵國和親霞扬。 傳聞我的和親對象是個殘疾皇子糕韧,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容