Android 極光推送快速開發集成指南

現在的APP可謂是不可沒有消息推送,這樣可以提高用戶的粘滯性,結合着大數據算法,推送一些關心的內容

消息推送的第三方SDK有很多 極光/個推/小米/華爲等,當然也可以公司開發一套消息推送

  下面是極光推送集成指南(之前做過很多次,一直沒有分享)

    setp1: 導入SDK並配置引用,然後引入各個平臺的so文件

 

然後在配置清單文件中加入:

<!-- since 3.0.9 Required SDK 核心功能 -->
<provider
    android:name="cn.jpush.android.service.DataProvider"
    android:authorities="包名"
    android:exported="true" />

<!-- since 1.8.0 option 可選項。用於同一設備中不同應用的 JPush 服務相互拉起的功能。 -->
<!-- 若不啓用該功能可刪除該組件,或把 enabled 設置成 false ;App 不會被其他 App 拉起,但會拉起其他的 App。 -->
<service
    android:name="cn.jpush.android.service.DaemonService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.DaemonService" />

        <category android:name="包名" />
    </intent-filter>
</service>

<!-- since 3.1.0 Required SDK 核心功能 -->
<provider
    android:name="cn.jpush.android.service.DownloadProvider"
    android:authorities="包名"
    android:exported="true" />

<!-- Required SDK 核心功能 -->
<receiver
    android:name="cn.jpush.android.service.PushReceiver"
    android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />

        <category android:name="包名" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
    <!-- Optional -->
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />

        <data android:scheme="package" />
    </intent-filter>
</receiver>

<!-- Required SDK 核心功能 -->
<activity
    android:name="cn.jpush.android.ui.PushActivity"
    android:configChanges="orientation|keyboardHidden"
    android:exported="false"
    android:theme="@android:style/Theme.NoTitleBar"
    tools:ignore="DuplicateActivity">
    <intent-filter>
        <action android:name="cn.jpush.android.ui.PushActivity" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="包名" />
    </intent-filter>
</activity>
<!-- SDK 核心功能 -->
<activity
    android:name="cn.jpush.android.ui.PopWinActivity"
    android:configChanges="orientation|keyboardHidden"
    android:exported="false"
    android:theme="@style/MyDialogStyle">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="包名" />
    </intent-filter>
</activity>

<!-- Since JCore2.0.0 Required SDK核心功能 -->
<!-- 可配置android:process參數將Service放在其他進程中;android:enabled屬性不能是false -->
<!-- 這個是自定義Service,要繼承極光JCommonService,可以在更多手機平臺上使得推送通道保持的更穩定 -->
<service
    android:name=".service.JpushBackgroudeService"
    android:enabled="true"
    android:exported="false"
    android:process=":pushcore">
    <intent-filter>
        <action android:name="cn.jiguang.user.service.action" />
    </intent-filter>
</service>

<!-- Required SDK 核心功能 -->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />

<!-- Required since 3.0.7 -->
<!-- 新的 tag/alias 接口結果返回需要開發者配置一個自定的廣播 -->
<!-- 3.3.0開始所有事件將通過該類回調 -->
<!-- 該廣播需要繼承 JPush 提供的 JPushMessageReceiver 類, 並如下新增一個 Intent-Filter -->
<receiver
    android:name=".receiver.PushBackgroudeBrocast"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />

        <category android:name="包名" />
    </intent-filter>
</receiver>

<!-- User defined. 用戶自定義的廣播接收器 -->
<!--
這是3.3.0之前版本的接收方式,3.3.0開始是通過繼承 JPushMessageReceiver並配置來接收所有事件回調。>
<!- 如果仍然需要在這個Receiver裏接收,需要在JPushMessageReceiver 的子類裏不重寫對應的回調方法,或者重寫方法且調用super
-->
 <receiver
 android:name="包名"
 android:enabled="true"
 android:exported="false">
 <intent-filter>
 <!--Required 用戶註冊 SDK 的 intent-->
 <action android:name="cn.jpush.android.intent.REGISTRATION" />
 <!--Required 用戶接收 SDK 消息的 intent-->
 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
 <!--Required 用戶接收 SDK 通知欄信息的 intent-->
 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
 <!--Required 用戶打開自定義通知欄的 intent-->
 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
 <!-- 接收網絡變化 連接/斷開 since 1.6.3 -->
 <action android:name="cn.jpush.android.intent.CONNECTION" />
 <category android:name="包名" />
 </intent-filter>
 </receiver>


<!-- User defined. 用戶自定義 Receiver 接收被拉起回調 -->
<!-- 自定義 Receiver 組件,繼承cn.jpush.android.service.WakedResultReceiver類,複寫onWake(int wakeType)或 onWake(Context context, int wakeType)方法以監聽被拉起 -->
<receiver android:name=".receiver.PushCallbackRegister">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.WakedReceiver" />

        <category android:name="${applicationId}" />
    </intent-filter>
</receiver>

<!-- Required SDK核心功能 since 3.3.0 -->
<activity
    android:name="cn.jpush.android.service.JNotifyActivity"
    android:exported="true"
    android:taskAffinity="jpush.custom"
    android:theme="@android:style/Theme.Translucent.NoTitleBar">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.JNotifyActivity" />

        <category android:name="包名" />
    </intent-filter>
</activity>

 

在gradle中的

defaultConfig{
ndk {
    abiFilters 'armeabi-v7a', 'x86', 'x86_64', 'armeabi-v7a'//,'arm64-v8a'
}

manifestPlaceholders = [
        JPUSH_PKGNAME: applicationId,
        JPUSH_APPKEY : "你的ID", //JPush 上註冊的包名對應的 Appkey.
        JPUSH_CHANNEL: "developer-default", //暫時填寫默認值即可.
]

}

這樣 配置就算可以了,然後在包下,複製從官網下載的demo,到包裏,參考地址:https://docs.jiguang.cn//jpush/client/Android/android_3m/

比如TagAliasOperatorHelper這個工具類 改好以後在全局的Applition中 初始化
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
String registrationID = JPushInterface.getRegistrationID(this);//這個可以傳給後臺 推送到具體的個人

 

 

如上配置好之後:

檢查一下有沒有通知欄權限

package xxx;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class CheckAndStartNitifycationUtils
{
    public static boolean isNotificationEnabled(Context context) {
        String CHECK_OP_NO_THROW = "checkOpNoThrow";
        String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

        AppOpsManager mAppOps = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        }
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;

        Class appOpsClass = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                appOpsClass = Class.forName(AppOpsManager.class.getName());
            }
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                    String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);

            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void startNotifyCationAction(Context context)
    {
//        boolean enabled = isNotificationEnabled(context);

//        if (!enabled) {
            /**
             * 跳到通知欄設置界面
             * @param context
             */
            Intent localIntent = new Intent();
            //直接跳轉到應用通知設置的代碼:
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                localIntent.putExtra("app_package", context.getPackageName());
                localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
            } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                localIntent.setData(Uri.parse("package:" + context.getPackageName()));
            } else {
                //4.4以下沒有從app跳轉到應用通知設置頁面的Action,可考慮跳轉到應用詳情頁面,
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.setAction(Intent.ACTION_VIEW);
                    localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
                    localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
                }
            }
            context.startActivity(localIntent);
        }
//    }
}

 

這樣就可以去調試了


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