Android中接入極光推送

Android中接入極光推送的步驟大體如下:

1.在主module的build.gradle中添加:

android {
    defaultConfig {
        manifestPlaceholders = [
            // app包名
            JPUSH_PKGNAME: "com.jiguang.test",
            // JPush上註冊的包名對應的appkey
            JPUSH_APPKEY : "898f0d6299c1666751ca40a1",
            // 道通,暫時填寫默認值即可
            JPUSH_CHANNEL: "developer-default",
        ]
    }
}
dependencies {
    ......
    implementation 'cn.jiguang.sdk:jpush:3.5.8'
    implementation 'cn.jiguang.sdk:jcore:2.3.0'
    ......
}

2.在主module的AndroidManifest.xml中添加:

<!-- 極光推送 begin -->
<!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process參數將Service放在其他進程中;android:enabled屬性不能是false -->
<!-- 這個是自定義Service,要繼承極光JCommonService,可以在更多手機平臺上使得推送通道保持的更穩定 -->
<service
    android:name=".service.JPushService"
    android:enabled="true"
    android:exported="false"
    android:process=":pushcore">
    <intent-filter>
        <action android:name="cn.jiguang.user.service.action" />
    </intent-filter>
</service>
<!-- Required since 3.0.7 -->
<!-- 新的 tag/alias 接口結果返回需要開發者配置一個自定的廣播 -->
<!-- 3.3.0開始所有事件將通過該類回調 -->
<!-- 該廣播需要繼承 JPush 提供的 JPushMessageReceiver 類, 並如下新增一個 Intent-Filter -->
<!-- category中的name爲app的包名 -->
<receiver
    android:name=".receiver.JPushReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
        <category android:name="com.jiguang.test" />
    </intent-filter>
</receiver>
<!-- 極光推送 end -->

3.自定義JCommonService類:

package com.jiguang.test.service;

import cn.jpush.android.service.JCommonService;

/**
 * 極光推送服務
 */
public class JPushService extends JCommonService {
    // 空實現即可
}

4.自定義JPushMessageReceiver類:

package com.jiguang.test.receiver;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.haier.uhome.uplustv.data.JPushBean;
import com.haier.uhome.uplustv.util.RemindUtil;
import com.haier.uhome.uplustv.util.UIUtil;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * 極光推送的廣播接收器
 */
public class JPushReceiver extends JPushMessageReceiver {
    private static final String TAG = JPushReceiver.class.getSimpleName();

    public JPushReceiver() {
    }

    /**
     * 自定義消息接收
     *
     * @param context       上下文
     * @param customMessage 自定義消息
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.d(TAG, "onMessage customMessage=" + customMessage);
    }

    /**
     * 通知消息接收
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageArrived notificationMessage=" + notificationMessage);
    }

    /**
     * 點擊通知消息
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageOpened notificationMessage=" + notificationMessage);
    }

    /**
     * 清除通知消息
     *
     * @param context             上下文
     * @param notificationMessage 通知消息
     */
    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        Log.d(TAG, "onNotifyMessageDismiss notificationMessage=" + notificationMessage);
    }

    /**
     * 註冊成功
     *
     * @param context        上下文
     * @param registrationId 註冊id
     */
    @Override
    public void onRegister(Context context, String registrationId) {
        Log.d(TAG, "onRegister registrationId=" + registrationId);
    }

    /**
     * 長連接狀態變化
     *
     * @param context     上下文
     * @param isConnected 長連接狀態,true表示已連接
     */
    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.d(TAG, "onConnected isConnected=" + isConnected);
    }
}

5.在主module的Application類的onCreate方法中添加:

JPushInterface.setDebugMode(true); // debug開關,傳true是打開debug模式
JPushInterface.init(this);


 

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