android集成极光推送以及初始化

android集成极光推送其实官方文档已经比较详细,
官方文档地址:http://docs.jiguang.cn/jpush/client/Android/android_guide/

以下记录自己集成的过程以及遇到的问题:

一、在极光官网注册账号和密码并进入
极光官网:https://www.jiguang.cn/

二、创建应用
在首页上选择开发者服务,选择推送服务进入创建应用界面
点击创建应用:
在这里插入图片描述
在其中输入应用名称,应用图标可不填

点击完成后:
在这里插入图片描述
其中:AppKey需要在自己的项目中使用到,稍后会详细的将到

然后点击完成推送设置,这一步是配置你需要收到推送的App的包名,
在这里插入图片描述
配置完后,就可以在项目中集成啦。

三、在项目中配置自动集成
当然,在官方文档中也有手动集成的介绍,但是很容易出错,而且也不太好使,还麻烦,个人建议会使用自动集成即可。

1、集成jar包
在集成之前需要看一下android studio 的 Project 根目录的主 gradle 中是否配置了 jcenter 支持

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

allprojects {
    repositories {
        jcenter()
    }
}

一般新建项目默认支持

在项目的build.gradle上添加jar包依赖和 AndroidManifest 的替换变量
在dependencies中添加:

implementation 'cn.jiguang.sdk:jpush:3.3.4'  // 此处以JPush 3.3.4 版本为例。
implementation 'cn.jiguang.sdk:jcore:2.1.2'  // 此处以JCore 2.1.2 版本为例。

2、在defaultConfig中添加:

扫描二维码关注公众号,回复: 9129813 查看本文章
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", //暂时填写默认值即可.
        ]
        ......
    }

其中:
applicationId: 就是你在创建应用时填写的包名
JPUSH_APPKEY :就是在创建应用时自动创建的AppKey

3、在跟目录的gradle.properties中添加:

android.useDeprecatedNdk=true

4、在AndroidManifest.xml文件中需要添加一个自定义service和一个自定义receiver广播接收器
自定义service:

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

这个service只需要继承JCommonService即可,不需要别的任何操作。不再多说

自定义receiver广播接收器

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

该广播接收器就是接收从极光上推送的通知,下面会给出该广播接收器中各种方法的具体用途
不多说直接给出整个类:

public class MyJPushReceiver extends JPushMessageReceiver {
    private static final String TAG = "JPushReceiver";

    /**
     * 收到自定义消息的回调
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        Log.d(TAG, "onMessage: " + customMessage.toString());
    }

    /**
     * 点击通知/打开通知的回调
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
        Log.d(TAG, "onNotifyMessageOpened: ");
    }

    /**
     * 收到通知的回调
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
        Log.i(TAG, notificationMessage.toString());
    }

    /**
     * 清除通知的回调
     */
    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
        Log.d(TAG, "onNotifyMessageDismiss: ");
    }
}

只需要在需要的回调方法里完成自己需要完成的逻辑即可

注:
1、在AndroidManifest.xml文件顶端package=“”要与在极光上注册的packageName一致
2、在MainActivity中要加入

JPushInterface.init(this);//初始化极光服务
JPushInterface.setDebugMode(true);//开启日志调试

不初始化极光服务不会收到通知

发布了43 篇原创文章 · 获赞 22 · 访问量 5954

猜你喜欢

转载自blog.csdn.net/qq_41466437/article/details/100600320