引入工具包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