极光推送3分集成干货--android

来源:互联网 发布:cdma无线网络优化流程 编辑:程序博客网 时间:2024/06/05 06:11

极光推送官方配置地址:点我阿

配置以下基本功能,如果还未能接受到推送,可前往极光社区寻求答案
极光推送基本配置

app.build.gradle文件中:

android {        ...        defaultConfig {            ...            // 极光推送配置 str            ndk {                //选择要添加的对应cpu类型的.so库。                abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'            }            manifestPlaceholders = [                    JPUSH_PKGNAME: applicationId,                    JPUSH_APPKEY : "您的key", //JPush上注册的包名对应的appkey.                    JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.            ]            // 极光推送配置 end            }        ...        }dependencies {    ...    // 极光推送配置 201742717:55:07    compile 'cn.jiguang.sdk:jpush:3.0.5'  // 此处以JPush 3.0.0 版本为例。    compile 'cn.jiguang.sdk:jcore:1.1.2'  // 此处以JCore 1.0.0 版本为例。}

manifest文件:

        <!-- 极光 str -->        <receiver            android:name="你自定义的Receiver"            android:enabled="true">            <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" />                <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />                <!-- 接收网络变化 连接/断开 since 1.6.3 -->                <action android:name="cn.jpush.android.intent.CONNECTION" />                <category android:name="您的包名" />            </intent-filter>        </receiver>        <!-- 极光 end -->

需要自定义一个Receiver

public class JPushNotificationReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        /*打印所有信息*/        //LogUtils.i("[JpushReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);            LogUtils.i("[JpushReceiver] 接收Registration Id : " + regId);            //send the Registration Id to your server...        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {            /*接收到推送下来的自定义消息*/            processCustomMessage(bundle);        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            LogUtils.i("[JpushReceiver] 接收到推送下来的通知");            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);            LogUtils.i("[JpushReceiver] 接收到推送下来的通知的ID: " + notifactionId);        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {            LogUtils.i("[JpushReceiver] 用户点击打开了通知");            ToastUtils.showToast("暂时跳转到这");            //打开自定义的Activity            Intent i = new Intent(context, PersonalDataActivity.class);            i.putExtras(bundle);//            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);            context.startActivity(i);        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {            LogUtils.i("[JpushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);            LogUtils.i("[JpushReceiver]" + intent.getAction() + " connected state change to " + connected);        } else {            LogUtils.i("[JpushReceiver] Unhandled intent - " + intent.getAction());        }    }    // 打印所有的 intent extra 数据    private static String printBundle(Bundle bundle) {        StringBuilder sb = new StringBuilder();        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))) {                    LogUtils.i("This message has no Extra data");                    continue;                }                try {                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));                    Iterator<String> it = json.keys();                    while (it.hasNext()) {                        String myKey = it.next().toString();                        sb.append("\nkey:" + key + ", value: [" +                                myKey + " - " + json.optString(myKey) + "]");                    }                } catch (JSONException e) {                    LogUtils.i("Get message extra JSON error!");                }            } else {                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));            }        }        return sb.toString();    }    //send msg to MainActivity    private void processCustomMessage(Bundle bundle) {        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); //消息内容        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); //附加字段        EventBus.getDefault().post(new NoticeMessageEvent(message)); //使用eventBus发送    }}

每一个手机唯一的极光ID:

JPushInterface.getRegistrationID(this)

最后在Application中初始化即可:

/** 极光初始化 */JPushInterface.setDebugMode(true);  // 设置开启日志,发布时请关闭日志JPushInterface.init(this);          // 初始化 JPush

好了,到这里您就完成了最简易的极光推送集成,如果需要更多推送功能您可以去官网API查看,如果有任何关于极光推送的问题均可去极光社区提问,也许社区首页就有项目中bug的解决方法。

2 0
原创粉丝点击