Android studio一分钟集成极光推送以及集成时遇到的混淆神坑

妹子我写代码很辛苦/(ㄒoㄒ)/~~ ,转载请注明出处哦~http://blog.csdn.net/u011791526/article/details/73457943 

作者catRuan(阮妹子)

联系方式:QQ:940472401 邮箱:[email protected]


最近项目用到极光推送,使用时遇到一些小问题,同时也get到一定经验,本文将展示如何使用Android studio  一分钟之内集成极光推送

1、首先奉上官方文档https://docs.jiguang.cn/jpush/client/Android/android_guide/#jcenter 

2、正式介绍集成方法

   ①、登录极光官网官网地址 注册并登录极光账号 (此步骤自己去实施吧)

   ②、进入极光开发者服务->创建应用->进入创建好的应用->保存一下appkey(项目里要用) ;进入创建好的项目->进入推送设置->选择Android->填写信息(注意包名书写正确,且保证module下的build.gradle中applicationId和manifest中的包名一致)。

   ③、打开项目module下的build.gradle ,在defaultConfig和dependencies添加如下信息

扫描二维码关注公众号,回复: 2875140 查看本文章
 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 : "自定义渠道名称", //用户渠道统计的渠道名称
        ]
        ......
    }
    ......
}

dependencies {
    ......

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

    ④、注意瞄一眼,是否配置了jcenter(查看项目的build.gradle的文件头处,默认是配置的),如下

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

allprojects {
    repositories {
        jcenter()
    }
}
   ⑤、没有第五点啦,重新build一下就ok ,至此极光已经集成完成~~~

   ⑥、如果想要定义自己的消息和广播接收器,只需要在manifest中添加如下代码

 <receiver
            android:name=".receiver.JPushReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <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  用户接收SDK通知栏信息的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required  用户打开自定义通知栏的intent -->
                <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <category android:name="com.cmrh.goa.android.demo" />
            </intent-filter>
        </receiver>
顺便附上我自己定义的receiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cmrh.goa.android.demo.app.Constants;
import com.cmrh.goa.android.demo.ui.setting.LogActivity;

import cn.jpush.android.api.JPushInterface;

public class JPushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Constants.REGISTRATION_ID = (String) bundle.get(JPushInterface.EXTRA_REGISTRATION_ID);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.i("JPushReceiver", "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏,完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.i("JPushReceiver", "收到了通知");
            // 在这里可以做些统计,或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.i("JPushReceiver", "用户点击打开了通知");
            // 在这里可以自己写代码去定义用户点击后的行为
            Intent i = new Intent(context, LogActivity.class);  //自定义打开的界面
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}
      ⑦、在application记得初始化 极光推送哦,也是非常简单
 JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
 JPushInterface.init(this);            // 初始化 JPush

好啦,现在你可以试着登录极光推送消息看看啦,绝对比极光给的demo还容易上手哦!


------------------------------------------------------分割线-----------------------------------------------------

下面介绍一下因为混淆引起极光无法工作的解决办法

当我们代码配置了混淆操作,但极光这边没有做恰当处理的话,就会报如下错误


解决方法,其实极光说的很明白了戳我,一定要照做哦

  • 请下载4.x及以上版本的proguard.jar, 并替换你Android Sdk "tools\proguard\lib\proguard.jar"

  • 请在工程的混淆文件中添加以下配置:

    -dontoptimize
    -dontpreverify
    
    -dontwarn cn.jpush.**
    -keep class cn.jpush.** { *; }
    
    -dontwarn cn.jiguang.**
    -keep class cn.jiguang.** { *; }
    
  • v2.0.5 ~ v2.1.7 版本有引入 gson 和 protobuf ,增加排除混淆的配置。(2.1.8版本不需配置)

    #==================gson && protobuf==========================
    -dontwarn com.google.**
    -keep class com.google.gson.** {*;}
    -keep class com.google.protobuf.** {*;}
其中, proguard.jar不一定能下载到,这个时候就需要小伙伴们翻墙去下了

这里提供大家一个5.3.3版本的jar包,请用http://pan.baidu.com/s/1nvr37Bj 提取码是nrc4



猜你喜欢

转载自blog.csdn.net/u011791526/article/details/73457943