【集成步骤】
jcenter 自动集成步骤
- 说明 :
1.使用jcenter自动集成的开发者,不需要在项目中添加jar和so,jcenter会自动完成依赖;
2.在AndroidManifest.xml中不需要添加任何JPush SDK 相关的配置,jcenter会自动导入。
1.(新建project默认配置就支持,此步可忽略)确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。
buildscript {
repositories {
jcenter()
}
......
}
allprojets {
repositories {
jcenter()
}
}
2.在 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.1.1' // 此处以JPush 3.1.1 版本为例。
compile 'cn.jiguang.sdk:jcore:1.1.9' // 此处以JCore 1.1.9 版本为例。
......
}
3.注册极光
一般在Application中执行
JPushInterface.init(getApplicationContext());
4.处理极光相关广播
附官方文档Android API
要想处理推送,可以自己实现相应的BroadcastReceiver ,并在AndroidManifest.xml中添加以下内容:
<receiver
android:name="你自己的广播"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="你的包名" />
</intent-filter>
</receiver>
附上对应的广播示例:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
public class JPushReceiver extends BroadcastReceiver {
private static final String TAG ="JPushTag";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "onReceive - " + intent.getAction());
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
}else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "收到了通知");
// 在这里可以做些统计,或者做些其他工作
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");
// 在这里可以自己写代码去定义用户点击后的行为
Intent i = new Intent(context, TestActivity.class); //自定义打开的界面
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
}
至此,集成基本完成。
demo
=====================2018.07.19更新 处理通知栏图标显示====================
最近遇到在google Pixel 8.1手机上,收到推送时,logo变成黑色的一块,如下图:
解决方式是,添加设置自定义通知栏样式
// 设置通知栏样式,处理在google手机上通知栏显示为黑色图标的问题
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(mContext);
builder.statusBarDrawable = R.mipmap.icon_logo;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; //可选,设置为可点击消失
builder.notificationDefaults = Notification.DEFAULT_SOUND; //可选,设置声音提示
JPushInterface.setDefaultPushNotificationBuilder(builder);
=====================2018.08.20更新 处理注销时不接收推送====================
由于app在登录时与服务端进行绑定,在注销时与服务端进行解绑,但是在解绑时可能遇到与服务端解绑不成功,导致用户退出还能收到推送,于是APP端这里还需调用其它API来解决。
以下是处理方式(mContext为上下文):
登录时:
JPushInterface.init(mContext); // 注册
if (JPushInterface.isPushStopped(mContext)) { // 判断是否停止接收推送消息
JPushInterface.resumePush(mContext); // 恢复接收推送 消息
}
注销时:
JPushInterface.clearAllNotifications(mContext); // 清除所有通知栏
JPushInterface.stopPush(mContext); // 停止接收推送消息