极光推送Jcenter自动集成过程

2019-06-28  本文已影响0人  取了个很好听的名字

前言

最近项目要接入消息推送,上次是手动接入的,这次使用了自动集成,该文就是集成过程的记录。

过程

创建应用

创建应用的过程在这里就不叙述了,不过在创建好应用后要记录下appkey,一会要用。


创建应用.png

Gradle配置

1.确认 android studio 的 Project 根目录的主 gradle 中配置了 jcenter 支持。(新建 project 默认配置就支持)

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

allprojects {
    repositories {
        jcenter()
    }
}

在 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.2'  // 此处以JPush 3.3.2 版本为例。
    compile 'cn.jiguang.sdk:jcore:2.0.1'  // 此处以JCore 2.0.1 版本为例。
    ......
}

注 : 如果在添加以上 abiFilter 配置之后 android Studio 出现以下提示:

NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin

则在 Project 根目录的 gradle.properties 文件中添加:

android.useDeprecatedNdk=true

注 : 使用 NDK r17 时,可能 Android Studio 会出现以下提示:

  A problem occurred starting process ‘command 
    ‘/Users/xxx/Library/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt
    /darwin-x86_64/bin/mips64el-linux-android-strip”

    系统找不到指定的文件

这是因为 NDK r17 之后不再支持 mips 平台,在 build.gradle 里增加如下配置可解决

android {

        defaultConfig {
            .....
        }

        packagingOptions { 
            doNotStrip '*/mips/*.so' 
            doNotStrip '*/mips64/*.so' 
        }
    }

AndroidManifest.xml配置

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

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

}

从JPush3.0.7开始,需要配置继承JPushMessageReceiver的广播,原来如果配了MyReceiver现在可以弃用。

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

    @Override
    public Notification getNotification(Context context, NotificationMessage notificationMessage) {
        return super.getNotification(context, notificationMessage);
    }

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
    }
    //接收到推送消息
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
    }

    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
    }
    //连接极光
    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        super.onCommandResult(context, cmdMessage);
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        super.onMultiActionClicked(context, intent);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }
}

在application中注册极光推送

 //初始化极光推送
    public void    initJpush(){
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

集成测试

测试结果如下:


测试结果.jpg
上一篇 下一篇

猜你喜欢

热点阅读