之前一直好奇項目中的推送是怎麼顯示到通知欄裏的,於是現在有時間就自己閱讀了一下文檔,集成了一下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();
}