Android studio中极光推送的快速集成

来源:互联网 发布:网络教育金融牌照 编辑:程序博客网 时间:2024/06/05 08:25

前言

极光推送(Jpush)是当下非常流行的一个消息推送框架,其在Android studio中的集成也更加的快速和简单,如果你手速够快,能在极短的时间内完成,这对于单身的程序猿来说并非难事。


1.配置project目录下的gradle

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

2.配置module下的gradle

android {    ......    defaultConfig {        applicationId "com.xxx.xxx" //JPush上注册的包名.        ......        ndk {            //选择要添加的对应cpu类型的.so库。            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-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.0.3'  // 此处以JPush 3.0.3 版本为例。    compile 'cn.jiguang.sdk:jcore:1.1.1'  // 此处以JCore 1.1.1 版本为例。    ......}

3.初始化Jpush


public class MyApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        //初始化Jpush        JPushInterface.setDebugMode(true);        JPushInterface.init(this);    }}


此时其实已经可以接收到推送了




但是如果你希望在收到通知后搞出一些其他事情,就需要配置一个广播接受者,在收到广播后作出相应的操作就可以了

4.自定义广播接受者

public class PushReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context,"接收推送",Toast.LENGTH_SHORT).show();    }}

在manifest中配置

<receiver android:name=".receiver.PushReceiver">    <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.zhuoxin.newsproject11" />    </intent-filter></receiver>

运行效果




大功告成!!!
原创粉丝点击