极光推送的配置,在官方文档中已经很详细地描述了;有手动导入跟jcenter导入两种方式,推荐使用jcenter;
//在gradle中添加
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush上注册的包名.
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
......
}
......
}
dependencis {
......
compile 'cn.jiguang:jpush:2.1.8' // 此处以SDK 2.1.8版本为例
......
}
下载官方的Demo,将里面的三个文件引入到自己的工程中;
然后,在启动界面注册极光推送,同时也可以修改默认的通知栏样式;
// 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。
private void initm() {
JPushInterface.init(getApplicationContext());
}
//for receive customer msg from jpush server
private MessageReceiver mMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = "com.zerom.clothesnews.MESSAGE_RECEIVED_ACTION";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
if (!JpushUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
}
}
}
}
public static void setNotification() {
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
MyApplication.getAppContext(),
R.layout.layout_notification, R.id.icon, R.id.title,
R.id.text);
builder.layoutIconDrawable = R.mipmap.ic_launcher;
builder.developerArg0 = "developerArg2";
JPushInterface.setPushNotificationBuilder(2, builder);
}
如果自己搭建后台服务器的话,需要设定一个字段,作为后天的识别;
JpushUtil.setAlias(context, "给后台的字段");
在JpushReceiver中可以对接收到的后台字段进行处理,比如说识别到是信息中心的字段就跳到信息中心,商品字段就跳到商品界面等等;
//接收到的后台的返回数据
String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
还有一种情况,就是推送的时候需要推送给特定的人,而不是所有的人;首先,在JpushUtil中添加多一个方法;
public static void setAliasAndTag(Context context, Set<String> tag) {
TagAliasCallback tagAliasCallback = new TagAliasCallback() {
@Override
public void gotResult(int i, String s, Set<String> set) {
}
};
JPushInterface.setAliasAndTags(context, null, tag, tagAliasCallback);
}
然后把每个用户的特定值设置为Tag,比如说我的app用的是用户id作为TAG,所以我在用户登录成功之后,则把用户ID设置为TAG,到时候后台根据TAG,也就是用户ID就可以推送给指定的人了;
Set<String> set = new HashSet<>();
String Tag = resultsBean.getUserid() + "";
set.add(Tag);
JpushUtil.setAliasAndTag(context, set);
做完上面的操作,就能接收到推送消息了,但是,有时候会发现,当我们把APP后台或者手机锁屏的时候,就收不到推送的消息了;解决办法很简单,因为极光推送是通过BroadcastReceiver调起通知栏的,所以只要动态注册BroadcastReceiver即可实现APP在后台运行的时候也能收到推送消息,而动态注册的方法则是通过新建一个Service,启动Service,通过Service动态注册BroadcastReceiver;
public class RegisterService extends Service {
public static boolean isForeground = false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
isForeground = true;
initm();
registerMessageReceiver();
JpushUtil.setAlias(this, "normal");
WelcomeActivity.setNotification();
}
@Override
public void onDestroy() {
super.onDestroy();
Intent localIntent = new Intent();
localIntent.setClass(this, RegisterService.class);
this.startService(localIntent);
}
// 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。
private void initm() {
JPushInterface.init(getApplicationContext());
}
private MessageReceiver mMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = "com.taorouw.MESSAGE_RECEIVED_ACTION";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
if (!JpushUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
}
}
}
}
}
最后在AndroidManifest中注册Service,并在启动界面启动Service;
startService(new Intent(WelcomeActivity.this, RegisterService.class));