Android集成推送

說起推送,這種第三方的平臺很多,每個公司都可能用的不一樣,之前我項目裏都是用的Jpush,具體怎麼配置就不多說了,不管用什麼平臺,就總結一點:集成的大體東西都基本一樣。最近手頭的這個項目用的是阿里雲的移動推送。就簡單說一下這個推送的集成。
不管哪個平臺,大致都要先去註冊應用,獲取相應的appkey和appsecret。
正文:
第一步:用Maven快速集成進行配置。
文檔地址:https://help.aliyun.com/document_detail/51056.html?spm=a2c4g.11186623.6.623.43da7fa8oUfFFH#h2–maven-1
進去照着配置就行了。就說一點:如果是快速集成(官方也建議採用此方式集成),Manifest不需要配置那些權限和其他相關組件配置,其他的不過多解釋。
也就說清單文件只需要配置如下兩項即可。

 <!-- 阿里雲推送相關(配置appkey和appsecret) -->
        <meta-data
            android:name="com.alibaba.app.appkey"
            android:value="你的appkey" />
        <meta-data
            android:name="com.alibaba.app.appsecret"
            android:value="你的appsecret" />
        <!-- 消息接收監聽器 (用戶可自主擴展) -->
        <receiver
            android:name=".receiver.MyMessageReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.alibaba.sdk.android.push.RECEIVE" />
            </intent-filter>
        </receiver>

自定義一個MyMessageReceiver用來接收通知的接收和點擊事件的相關處理。配置就算完成了。
第二步:初始化SDK
官方文檔也說明了,初始化放在application中。如下:

private void initCloudChannel(AppApplication applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.e(TAG, "init cloudchannel success");
//                Log.e("===id", pushService.getDeviceId() + "");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }

第三步:綁定推送標識
這個自行決定用什麼字段作爲推送的唯一標識,這裏我用的是user_id。在獲取到user_id的地方綁定即可。如下:

//阿里雲推送綁定賬戶
PushServiceFactory.getCloudPushService().bindAccount(user_id, new CommonCallback() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onFailed(String s, String s1) {

            }
        });

第四步:接收推送內容,處理點擊事件
這個主要就是看Recriver了,就是上面定義的MyMessageReceiver,代碼如下:

public class MyMessageReceiver extends MessageReceiver {

    /**
     * 接收到通知的回調
     */
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        //8.0適配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }

    /**
     * 點擊通知的回調
     */
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        try {
            JSONObject object = new JSONObject(extraMap);
            String param= object.optString("param");
            if (!TextUtils.isEmpty(param) ) {
                Intent intent = new Intent(context, XXXActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("param", param);
                context.startActivity(intent);
            }
        } catch (JSONException ignored) {
        }
    }
}

這裏要注意Android8.0通知的顯示和點擊事件問題,8.0以上必須要設置通知渠道才能正常顯示通知,不然收到了通知也顯示不出來。
適配8.0 的通知主要注意以下幾點:
1:必須設置通知渠道和渠道name,都不可爲空,缺一不可,不然不顯示。
2:必須設置setSmallIcon,不然不顯示。
3:設置setContentIntent一定要在notify之前。不然不顯示。

關於推送參數的獲取,可以通過extraMap來解析獲取就可以了。然後添加到intent中傳遞到目標Activity即可。

還要額外注意一點:關於在跳轉目標Activity中參數的接收。
生命週期有可能會走:onCreate,也有可能會走:onNewIntent。因此最好的方法就是先在這兩個生命週期中,都定義好處理通知的方法dealNotice(Intent intent);,最後統一處理即可。

第五步:必要的時候進行解綁(比如退出登錄了)

PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
                @Override
                public void onSuccess(String s) {
                    Log.e("===unbindSuccess", s);
                }

                @Override
                public void onFailed(String s, String s1) {

                }
            });

在實際項目中,發送9.0的手機初始化就有問題,提示網絡配置錯誤,讓檢查網絡配置,這實際上就是阿里的推送沒適配9.0系統的https網絡安全。解決辦法網上也是有的,很簡單,如下:
清單文件的application節點下添加:

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