安卓极光推送--AndroidStudio 集成极光推送

有些东西时间久了就都忘记了,写博客的好处大概就是总结一下,近来项目没那么紧,想着把一些第三方的集成再来一遍。仅集成成功,简单demo。极光推送大家都用过,了解会用的就跳过吧,此篇仅针对初学者或者忘记巩固者。

关于极光的申请就不说什么了  ,重要的一点是项目中的包名和再极光申请时要保持一致 拿到极光的appkey即可。

关于Studio的配置接下来分为几步:

一:在项目的Module的build.gradle中:

defaultConfig {
    applicationId "com.example.administrator.jpushdemo"
    minSdkVersion 19  //在15以上
    targetSdkVersion 27
    versionCode 1
    versionName "1.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 : "29f7c75b54ad66ce10268cc8", //JPush上注册的包名对应的appkey*换成你的*            JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
    ]
}
在
dependencies{
compile 'cn.jiguang:jpush:2.1.8'//使用最新的最好
}

二:在MyApplication中初始化  记得再AndroidManifest.xml中配置name属性

自定义广播接收者再AndroidManifest.xml中进行配置如下:
<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用户注册SDKintent-->
        <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.example.administrator.jpushdemo" />//自己应用的包名
    </intent-filter>
</receiver>

三:配置结束  可以撸代码了:

在自定义的广播接受者中:

 @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

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

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

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
} 
常用的应该就这几个。就是这么简单 点击打开链接源码下载

猜你喜欢

转载自blog.csdn.net/u010354511/article/details/80483797