在整合極光推送後,根據不同推送內容跳轉App相應的頁面
阿新 • • 發佈:2019-02-07
前言:由於某push到達率問題,所以我們決定換成口碑較高的極光推送,在此梳理一下完成過程,小菜鳥剛剛起步,不足之處還請各位多多指教~
需求:1.整合極光推送
2.根據推送內容的不同跳轉相應的頁面,也就是獲取推送內容並作出處理(非官網中所說的Tag使用)
附加:檢測使用者對app是否開啟了通知功能
鐺鐺鐺鐺~下面開始拙劣的表演~
一.整合
極光推送整合文件(灰常詳細~)
本豆使用的開發工具是AS3.0.1(eclipse已經忘差不多了-_-!)整合方法採用jcenter 自動整合,
官網整合步驟寫的灰常詳細就不重複說明了(PS:如果說也是把人家官網的粘過來。。。)
二.在“成功”整合推送之後,根據推送內容跳轉頁面
1.建立一個java類 JpushReciver 繼承 BroadcastReceiver 重寫 onReceive方法
2.根據需求寫入相應邏輯
public class MyReceive extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { //官網提供根據Registration ID 進行推送 此方法用於處理該類推送訊息 }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { // 自定義訊息不會展示在通知欄,完全要開發者寫程式碼去處理 // 處理自定義訊息 } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { //接收到通知會走的方法 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { //使用者點選通知會走的方法 //獲取推送訊息的方法 String content = bundle.getString(JPushInterface.EXTRA_ALERT); // 在這裡可以自己寫程式碼去定義使用者點選後的行為 if(context != null){ //例如 如果推送內容以【訊息】開頭 則點選後跳轉到訊息的Activity 否則跳轉到主頁面 if(content.startWith("【訊息】")){//判斷內容的條件 Intent i = new Intent(context, ChatActivity.class); //開啟訊息介面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); }else{ Intent i = new Intent(context, MainActivity.class); //開啟主介面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }else{ Log.d("-------","null"); } } else { //... } }
}
3.把所建立的java類在AndroidManifest中新增如下宣告:
<receiver
android:name="1中你建立的java類名"
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>
附加:檢測使用者對app是否開啟了通知功能 如下圖所示效果 點選前往開啟跳轉至設定介面 使用者可手動開啟通知
程式碼:
在事件中新增 checkNotice(); 方法
(PS:我是在Fragment中的獲取使用者配置資訊成功後呼叫的,切記不要在onCreateView中呼叫有關pop方法)
//宣告變數
private int on = 0;//用於控制開啟一次app提示只顯示一次
private View contentView;
private PopupWindow window;
......
//此方法用於檢測是否開啟通知
private void checkNotice() {
NotificationManagerCompat manager = NotificationManagerCompat.from(getActivity().getApplicationContext());
//判斷裝置是否開啟通知功能
boolean isOpened = manager.areNotificationsEnabled();
if(!isOpened && on == 0){
//顯示popupwindow提示框
showNoticePop();
}else {
return;
}
}
//設定popupwindow的方法
private void showNoticePop() {
on = 1;
contentView = LayoutInflater.from(getActivity()).inflate(R.layout.item_pop_notice, null);
window = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Button out= (Button) contentView.findViewById(R.id.btn_out_true);
Button fout= (Button) contentView.findViewById(R.id.btn_out_false);
window.setBackgroundDrawable(new ColorDrawable(0x00000000));
window.setOutsideTouchable(true);
window.setTouchable(true);
setBackgroundAlpha(0.5f);
window.showAtLocation(getView(), Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
//pop中前往開啟通知的監聽
out.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toOpenNotice(getActivity());
window.dismiss();
}
});
//pop中拒絕前往開啟通知的監聽
fout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showShortToast(getActivity(),"很遺憾,以後有訊息不能及時通知您了~");
window.dismiss();
}
});
//pop關閉後 介面顏色設定
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackgroundAlpha(1f);
}
});
}
//不同系統前往開啟通知的適配
private void toOpenNotice(Context context) {
Intent intent = new Intent();
//android 8.0
if(Build.VERSION.SDK_INT >=26){
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
}
//android 5.0-7.0
if(Build.VERSION.SDK_INT >=21 && Build.VERSION.SDK_INT <26) {
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
}
//其他
if(Build.VERSION.SDK_INT <21){
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
//頁面背景效果設定
public void setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = bgAlpha;
getActivity().getWindow().setAttributes(lp);
}
本集完~