之前一直好奇项目中的推送是怎么显示到通知栏里的,于是现在有时间就自己阅读了一下文档,集成了一下Jpush 。
Jpush提供了4中类型的消息:通知、自定义消息、富媒体和本地通知
Jpush 的通知类型的消息就是一种notification,可以直接显示到Android手机的通知栏上,通过查阅文档发现是否显示到手机的通知栏和显示的样式都是可以修改
详细的可以查看(https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification)
集成过程:之前自己做过手动集成,发现比较繁琐,这次就选择了 jcenter自动集成的方法
1、确保project的主gradle中配置了jcenter的集成
buildscript {
repositories {
jcenter()
}
......
}
allprojects {
repositories {
jcenter()
}
}
2、在 module的gradle中添加配置文件的配置变量和依赖
- defaultConfig {
- //极光推送
- ndk {
- //选择要添加的对应cpu类型的.so库。
- abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
- // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
- }
- manifestPlaceholders = [
- JPUSH_PKGNAME: "集成推送的包名",
- JPUSH_APPKEY : "申请的appkey", //JPush上注册的包名对应的appkey.
- JPUSH_CHANNEL: "developer-default", //用户渠道统计的渠道名称
- ]
- }
- dependencies{
- compile 'cn.jiguagn.sdk:jpush:3.0.5'
- compile 'cn.jiguang.sdk.jcore:1.1.2'
- }
3、配置后build_gradle,如果出现这个问题
NDK integration is deprecated in the current plugin......在gradle_properties中添加
android.usdDeprecatedNdk=true
4、配置清单文件
empty
5、在app的application中初始化jpush
JpushInterface.setDebugMode(true);
JpushInterface.init(this);
6、设置别名(个人认为是后台推送消息区分用户的标识)
public class App extends Application {
private static final int MSG_SET_ALIAS = 1001;
public static SharedPreferences preference;
public static SharedPreferences.Editor editor;
@Override
public void onCreate() {
super.onCreate();
preference = getSharedPreferences("config", Context.MODE_PRIVATE);
editor = preference.edit();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
/**
* 设置别名
*/
private void setAlies(String alies) {
// 调用 Handler 来异步设置别名
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alies));
}
private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
@Override
public void gotResult(int code, String alias, Set
tags) {
String logs ;
switch (code) {
case 0:
logs = "Set tag and alias success";
Log.i("tag", logs);
editor.putBoolean("jpushAlias", true).commit();
break;
case 6002:
logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
Log.i("tag", logs);
// 延迟 60 秒来调用 Handler 设置别名
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
break;
default:
logs = "Failed with errorCode = " + code;
Log.e("tag", logs);
}
// ExampleUtil.showToast(logs, getApplicationContext());
}
};
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_SET_ALIAS:
Log.d("tag", "Set alias in handler.");
// 调用 JPush 接口来设置别名。
JPushInterface.setAliasAndTags(getApplicationContext(),
(String) msg.obj,
null,
mAliasCallback);
break;
default:
Log.i("tag", "Unhandled msg - " + msg.what);
}
}
};
}
7、处理接收到的推送、比如点击通知栏跳转到指定的activity(自选)
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 处理 处理自定义
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
String result = bundle.getString(JPushInterface.EXTRA_EXTRA);
com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
String str = obj.getString("type");
if ("oa".equals(str)) {
if (MyApplication.getInstance().getUser() != null &&
!TextUtils.isEmpty(MyApplication.getInstance().getUser().get_id())) {
//打开自定义的Activity
Intent i = new Intent(context, Activity_Test.class);
i.putExtras(bundle);
//在APP外部打开内部activity需添加flag
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
} else {
Intent i = new Intent(context, Activity_login.class);
context.startActivity(i);
}
}
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
Log.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator
it = json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " + json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}