极光推送快速集成

极光推送快速集成

导读:快速集成只需5步即可完成,官方文档中的jcenter 自动集成步骤,但是看文档要花时间,以后遇到之间看这里就可以了,方便提高效率。

第一步 配置
module中Buile.Gradle的defaultconfig配置

ndk {


            //选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }

        manifestPlaceholders = [
            JPUSH_PKGNAME : applicationId,
            JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
            JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
        ]

第二步 添加依赖
Build.gradle的dependencies添加依赖

dependencies {
    compile 'cn.jiguang.sdk:jpush:3.0.9'  // 此处以JPush 3.0.9 版本为例。
    compile 'cn.jiguang.sdk:jcore:1.1.7'  // 此处以JCore 1.1.7 版本为例。
}

第三步 初始化SDK
application中初始化SDK,记得一定在AndroidMainfest.xml配置application的name属性

public class ExampleApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
}

第四步 创建广播接收消息
创建Receiver广播接收消息(复制极光推送demo里的MyReceiver)
创建Receiver里部分要改的代码

 else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

                String data = printBundle(bundle);
                Intent i = new Intent();
                if(data != null){
                    if(data.contains("adddepedency")){
                        i.setClass(context, ApplyMessageActivity.class);
                    }else if(data.contains("message")){
                        i.setClass(context, YYMMessageAtivity.class);
                    }
                }else{
                    i.setClass(context, YYMMessageAtivity.class);
                }
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                context.startActivity(i);

            }
// 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        String yymType = "";
        for (String key : bundle.keySet()) {

            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    yymType = json.toString();
                    Iterator<String> it =  json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " +json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        Log.e("===sll=Jpush",sb.toString()) ;
        return yymType;
    }

第五步 配置广播
AndroidManifest.xml配置自定义的广播

        <receiver
            android:name=".receiver.MyReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <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  用户接收SDK通知栏信息的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required  用户打开自定义通知栏的intent -->
                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->
                <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <category android:name="com.telehot.yym" />
            </intent-filter>
        </receiver>

总结
1.完成以上五步就可以测试了,特别强调别忘了打开手机应用通知权限。
2.后端需要设置别名

//添加别名
        String code = aCache.getAsString("userCode");
        JPushInterface.setAlias(this, 0, code);

//退出登录删除别名  
        JPushInterface.deleteAlias(this, 0);

猜你喜欢

转载自blog.csdn.net/song_liang_liang/article/details/81219329