极光推送快速集成篇

1.首先导入依赖:

    //极光推送
    implementation 'cn.jiguang.sdk:jpush:3.1.1'
    implementation 'cn.jiguang.sdk:jcore:1.1.9'

2.在你项目的build.gradle中

defaultConfig {
        applicationId "极光推送注册的包名"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //极光推送
        ndk {
            //选择要添加的对应cpu类型的.so库(不需要的删除即可)。
            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'
        }

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

    }

3.在AndroidManifest.xml清单文件中配置:


    

    
    
    
    
    
    
    
    
    
    

在你的清单文件的Application中配置

-

-

 

-

 

-

 

 

-

 

-

 

-

 

-

 

 

 

自己定义的一个receiver

 

4.配置完成在你的项目Application启动时候注册jpush

JPushInterface.init(this);

5.自定义的MessageReceiver 类

public class MessageReceiver extends BroadcastReceiver {

    private static final String TAG = "JPush";

    @Override
    public void onReceive(final Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + bundle.toString());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            String contentstr = bundle.getString(JPushInterface.EXTRA_ALERT);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.e(TAG + "接收到的推送数据是::", contentstr);
            Log.e(TAG + "接收到的推送数据是::", extras);
            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
           
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 用户点击打开了通知");
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Log.e("tag_获得的推送消息", extras);

        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 用户收到到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);
            Log.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.e(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

}

6.不要忘记手机添加通知权限!!!重点哟

可能写的有些遗漏,好久之前做的了,附上官方文档一份

http://docs.jiguang.cn/jpush/client/Android/android_guide/

你可能感兴趣的:(极光推送快速集成篇)