JPush Android SDK快速集成

刚刚通过Jcent集成JPush的时候遇到些麻烦。主要是因为官方文档里面自动集成和手动集成混在一起写,不清楚自动添加哪些部分需要添加,所以写篇博客记录下,Jcent自动集成JPush的方法。官方文档请看这里,本文章只是快速编写案例的Demo。另外本文是基于jpush3.3.0以及jcore2.0.1版本,JPush更新了版本的话,可能配置方式也会有相应的改变。

1. 在Android Studio的Project根目录中的主gradle中配置jcenter只支持。(如果已经有了 ,可以忽略本步骤)

buildscript {
    repositories {
        jcenter()
    }
    ......
}

allprojects {
    repositories {
        jcenter()
    }
}

2. 在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量。 

android {
    ......
    defaultConfig {
        applicationId "com.xxx.xxx" //JPush 上注册的包名.
        ......

        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", //暂时填写默认值即可.
        ]
        ......
    }
    ......
}

dependencies {
    ......

    compile 'cn.jiguang.sdk:jpush:3.3.0'  // 此处以JPush 3.3.0 版本为例。
    compile 'cn.jiguang.sdk:jcore:2.0.1'  // 此处以JCore 2.0.1 版本为例。
    ......
}

3. 如果你使用的 JCore 是 2.0.0 及以上的版本,需要额外在 Androidmanifest 中配置一个Service,以在更多手机平台上获得更稳定的支持,示例如下。(JCore1.x版本不需要,这里用的是2.0.1所以是需要的)

<!-- Since JCore2.0.0 Required SDK核心功能-->
 <!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
 <!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
 <service android:name="xx.xx.XService"
         android:enabled="true"
         android:exported="false"
         android:process=":pushcore">
         <intent-filter>
             <action android:name="cn.jiguang.user.service.action" />
         </intent-filter>
 </service>

 <!-- Required since 3.0.7 -->
 <!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 -->
 <!-- 3.3.0开始所有事件将通过该类回调 -->
 <!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
 <receiver
       android:name="自定义 Receiver"
       android:enabled="true" 
       android:exported="false" >
       <intent-filter>
            <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
            <category android:name="您应用的包名" />
       </intent-filter>
 </receiver>

service和receiver分别继承自JCommonService和JPushMessageReceiver,可以有控实现,如:

public class MyReceiver extends JPushMessageReceiver {
}
public class MyService extends JCommonService {
}

如果出错了,可以查看官方的集成文档,里面有解释,本文是快速集成,为了文档简洁就不添加了。

4. 在Application中初始化Jpush,这一点很容易本忽视。而且可能会被官方的手动集成文档误导,加了其他内容,其实并不需要,直接初始化就可以了。

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

注意需要在AndroidManifest.xml中配置该Application,如:

 <application
     android:name=".app.ExampleApplication"
     android:allowBackup="true"
     android:icon="@mipmap/ic_launcher"
     android:label="@string/app_name"
     android:supportsRtl="true"
     android:theme="@style/AppTheme"
     tools:ignore="GoogleAppIndexingWarning">
</application>

5. 此时App里面的集成已经完成了,接着只要在JPush的后台发送消息即可,接受到消息之后更多的处理还是看官方文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值