Android-极光推送集成流程

一、添加依赖

//极光推送
compile 'cn.jiguang.sdk:jpush:3.0.0'
compile 'cn.jiguang.sdk:jcore:1.0.0'

二、配置参数

//defaultConfig{
    JPUSH_PKGNAME : applicationId,
    JPUSH_APPKEY : "501e0f31b4e163e1", //JPush上注册的包名对应的appkey.
    JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.   
}

三、配置清单文件

1.添加必要的权限
2.注册广播接受者
    <!-- User defined. 用户自定义的广播接收器 -->
    <receiver
        android:name=".bookstore.receiver.PushReceiver"
        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"/>
            <!-- 接收网络变化 连接/断开 since 1.6.3 -->
            <action android:name="cn.jpush.android.intent.CONNECTION"/>

            <category android:name="com.laikan.reader"/>
        </intent-filter>
    </receiver>

四、创建Receiver类

    public class PushReceiver extends BroadcastReceiver{
    private static final String TAG = "PushReceiver";

    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("lyt","receiver called");
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
    //     Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");

            receivingNotification(context,bundle);

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

    //      openNotification(context,bundle);
            openLaikan(context);

        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

五、在Application中初始化Jpush

JPushInterface.init(this);
JPushInterface.setDebugMode(true);  // 极光推送 设置开启日志,发布时请关闭日志
    原文作者:一份饥
    原文地址: https://www.jianshu.com/p/2f5bafaa8a17
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞