Android集成華爲推送的問題總結

前言

       公司最近想要把幾個主流平臺的推送進行一下整合,做一個DEMO出來,方便之後使用.做安卓開發的童鞋應該也都應該瞭解,自定義的服務基本上在APP被殺掉後也會被幹掉(沒有設置後臺保護的情況下),所以要保證推送的及時和不失真,最好的方法還是去各大主流的手機廠商開發者網站去註冊申請推送服務.

總結

      關於集成華爲推送的部分這裏就不做介紹了.主要對集成華爲推送服務後遇到的問題進行記錄總結.希望可以幫助到同樣遇到問題的童鞋.

總結一:

華爲推送的初始化依賴Activity,所以不能在Application中進行初始化.


總結二:

華爲推送服務中提供的透傳消息是可能會丟失的.(當APP被殺死後,透傳消息有時是收不到的,並且在重新啓動APP後,透傳消息也是收不到的,所以不推薦使用透傳去做一些重要的功能)


總結三:

先看一下官方的Recevier:

public class HuaweiPushReceiver extends PushReceiver {
    private static final String TAG = "Huawei PushReceiver";

    /**
     * 連接上華爲服務時會調用,可以獲取token值
     *
     * @param context
     * @param token
     * @param extras
     */
    @Override
    public void onToken(Context context, String token, Bundle extras) {
        String belongId = extras.getString("belongId");
        String content = "get token and belongId successful, token = " + token + ",belongId = " + belongId;
        Log.e(TAG, content);
    }

    /**
     * 透傳消息的回調方法
     *
     * @param context
     * @param msg
     * @param bundle
     * @return
     */
    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        try {
            String content = "-------Receive a Push pass-by message: " + new String(msg, "UTF-8");
            Log.e(TAG, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 自定義的消息的回調方法
     *
     * @param context
     * @param event
     * @param extras
     */
    @Override
    public void onEvent(Context context, PushReceiver.Event event, Bundle extras) {
        PushManager.getInstance().notifyPush(extras.getString(BOUND_KEY.pushMsgKey));
        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
            int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
            if (0 != notifyId) {
                NotificationManager manager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notifyId);
            }
            String content = "--------receive extented notification message: " + extras.getString
                    (BOUND_KEY.pushMsgKey);
            Log.e(TAG, content);
        }
        super.onEvent(context, event, extras);
    }

    /**
     * 連接狀態的回調方法
     *
     * @param context
     * @param pushState
     */
    @Override
    public void onPushState(Context context, boolean pushState) {
        try {
            String content = "---------The current push status: " + (pushState ? "Connected" :
                    "Disconnected");
            Log.e(TAG, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

對消息的監聽,提供了4種回調方法,註釋寫得很清楚,官網也有相關的解釋,不多說.這裏說一下onEvent方法,當時試了很多次沒試出這個回調如何觸發(可能是自己粗心大意),這裏記錄下,希望能幫到同樣粗心大意的童鞋吧.看下圖

這裏寫圖片描述

注意右邊的添加按鈕.在填完鍵和值之後一定要添加上!不然是沒有效果的.還有就是推送消息收到後,只有點擊這條消息的時候纔會走onEvent方法.


總結四:

當APP接收到推送後,點擊消息,會發現總是會先打開啓動頁,這個問題要怎麼解決呢?翻了翻文檔,仔細看了一下後臺發送推送時頁面,會看到如下內容:

這裏寫圖片描述

選擇直接打開程序的話會走一遍啓動頁.所以只能來研究一下這個自定義動作了,但是不知道填什麼,這裏會有一個例子,不知道有多少童鞋看不懂哈,反正開始我沒看明白(其實這裏邊用到了Content Provider方面的知識,項目中用到的比較少,所以有一些遺忘了,希望同樣沒看明白的童鞋可以去溫習一下).這裏直接寫一下步驟,首先在AndroidManifest中需要跳轉的Activity中加入

        <!-- 需要跳轉到的頁面-->
        <activity
                android:name=".demo.activity.PushDemoActivity"
                android:screenOrientation="portrait">
            <intent-filter>

                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:host="com.myhost.push"
                      android:path="/push_detail"
                      android:scheme="myscheme"/>
            </intent-filter>
        </activity>

之後在Activty中打印出來需要的uri就可以了,如下

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myscheme://com.myhost.push/push_detail?message=what"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String intentUri = intent.toUri(Intent.URI_INTENT_SCHEME);
        Log.e("PushDemoActivity","action是:" + intentUri);

這個得到的intentUri就是需要填到自定義動作中的內容,大概樣子是
intent://com.myhost.push/push_detailmessage=what#Intent;scheme=myscheme;action=android.intent.action.VIEW;launchFlags=0x10000000;end

當然scheme,host這些可以根據項目的功能模塊去修改名字,這裏只是舉個例子.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章