极光JPush推送模块封装使用

 

引入工具包jpushlib之后

1.在app项目里面的build.gradle 添加以下配置,下面代码是替换AndroidManifest.xml的currentPackageName字符串为当前包名
 
  android.applicationVariants.all { variant ->
           variant.outputs[0].processManifest.doLast {
               def manifestFile = "${buildDir}/intermediates/manifests/full/${variant.outputs[0].dirName}/AndroidManifest.xml"
               def updatedContent = new File(manifestFile).getText('UTF-8')
                       .replaceAll("com.kawa.jpushlib.","")
                       .replaceAll("currentPackageName", "${applicationId}")
                       .replaceAll("my_Receiver","广播接收者路径") //广播接收者路径
                       .replaceAll("my_jpush_appkey","极光appkey") //极光appkey
               new File(manifestFile).write(updatedContent, 'UTF-8')
           }
       }
 

 

 

注意: 可能找不到清单文件问题  
  3.x 用 ${variant.dirName}  
  3.0以下用 ${variant.outputs[0].dirName


2.在app项目里面的Application进行初始化

 

 

      JPushClient.getInstance(this);
3.在你的项目里面定一个广播接收文件,用于处理极光推送过来的数据, 
广播接收者路径:
com.kawa.jpushdemo.broadcast.JPushBroadcastReceiver    
public class JPushBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.e("tag",">>>>>>>有接收到数据<<<<<<<");
        //打开消息
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            if (bundle.getString(JPushInterface.EXTRA_EXTRA) != null) {
               //TODO 处理推送过来的数据
                Log.e("tag",">>>>>>>"+bundle.getString(JPushInterface.EXTRA_EXTRA));
            }
        }
        //有推送消息进来
        else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            //TODO 处理推送过来的数据
            Log.e("tag",">>>>>>>"+bundle.getString(JPushInterface.EXTRA_EXTRA));
        }
    }
}
4.引入jpushlib只有按照上面2个步骤就可以获得监听广播

 

2019年3月4日更新:

gradle里面之前的方法找不到清单文件路径,现在更新最新的

def manifestFile = "${manifestOutputDirectory}/AndroidManifest.xml"

上面可以获取到清单文件的路径

出处:https://developer.android.com/studio/known-issues

 

 

github地址:https://github.com/KawaLan/JpushDemo

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值