第一篇部落格:極光推送以及自定義推送聲音
阿新 • • 發佈:2019-01-09
極光註冊就不說了,首先配置App的build.grandle
在defaultConfig裡貼上一下內容appkey為我在極光註冊之後的Appkey
然後配置AndroidManifest 把下面這些資訊放在application裡面
<!--極光的--> <!-- Required SDK 核心功能 --> <!-- 可配置android:process引數將PushService放在其他程序中 --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- since 3.0.9 Required SDK 核心功能 --> <provider android:authorities="com.hemai.rider.DataProvider" android:name="cn.jpush.android.service.DataProvider" android:exported="true"/> <!-- since 1.8.0 option 可選項。用於同一裝置中不同應用的JPush服務相互拉起的功能。 --> <!-- 若不啟用該功能可刪除該元件,將不拉起其他應用也不能被其他應用拉起 --> <service android:name="cn.jpush.android.service.DaemonService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="cn.jpush.android.intent.DaemonService" /> <category android:name="com.hemai.rider" /> </intent-filter> </service> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true"> <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <category android:name="com.hemai.rider" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <!-- Optional --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Required SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.hemai.rider" /> </intent-filter> </activity> <!-- SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:theme="@style/MyDialogStyle"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.hemai.rider" /> </intent-filter> </activity> <!-- Required SDK核心功能 --> <service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" /> <!-- Required SDK核心功能 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" /> <!-- Required since 3.0.7 --> <!-- 新的tag/alias介面結果返回需要開發者配置一個自定的廣播 --> <!-- 該廣播需要繼承JPush提供的JPushMessageReceiver類, 並如下新增一個 Intent-Filter --> <receiver android:name="com.hemai.jpush.MyJPushMessageReceiver" android:enabled="true"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="com.hemai.rider" /> </intent-filter> </receiver> <!-- User defined. 使用者自定義的廣播接收器 --> <receiver android:name="com.hemai.jpush.MyReceiver" android:enabled="true"> <intent-filter> <!-- Required 使用者註冊SDK的intent --> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 使用者接收SDK訊息的intent --> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 使用者接收SDK通知欄資訊的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 使用者開啟自定義通知欄的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- 接收網路變化 連線/斷開 since 1.6.3 --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="com.hemai.rider" /> </intent-filter> </receiver>
然後在專案的根目錄建立一個jpush的包在裡面建立五個類
首先來說MyJpushMessageReceiver
/** * 自定義JPush message 接收器,包括操作tag/alias的結果返回(僅僅包含tag/alias新介面部分) * */ public class MyJPushMessageReceiver extends JPushMessageReceiver { @Override public void onTagOperatorResult(Context context,JPushMessage jPushMessage) { TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage); super.onTagOperatorResult(context, jPushMessage); } @Override public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){ TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage); super.onCheckTagOperatorResult(context, jPushMessage); } @Override public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) { TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage); super.onAliasOperatorResult(context, jPushMessage); } }
然後是自定義的接收器Myreciever
** * 自定義接收器 * <p> * 如果不定義這個 Receiver,則: * 1) 預設使用者會開啟主介面 * 2) 接收不到自定義訊息 */ public class MyReceiver extends BroadcastReceiver { private static final String TAG = "tag"; @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); Logger.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); Logger.e(TAG, "[MyReceiver] 接收Registration Id : " + regId); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下來的自定義訊息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); //processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下來的通知"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); //bundle.get(JPushInterface.EXTRA_ALERT);推送內容 Logger.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId); //獲取到的推送的通知型別 String str_Test = bundle.getString(JPushInterface.EXTRA_ALERT); Log.e("tag", "測試:" + str_Test); //在這裡自定義通知聲音 processCustomMessage(context,bundle); //這裡通過EventBus來想我需要更新資料的介面傳送更新通知 EventBus.getDefault().postSticky(new MessageEvent(str_Test)); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 使用者點選打開了通知"); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Logger.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); Logger.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); } } catch (Exception e) { } } // 列印所有的 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 (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) { Logger.i(TAG, "This message has no Extra data"); continue; } try { JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); Iterator<String> it = json.keys(); while (it.hasNext()) { String myKey = it.next(); sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]"); } } catch (JSONException e) { Logger.e(TAG, "Get message extra JSON error!"); } } else { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString(); } /** * 自定義推送的聲音 * @param context * @param bundle */ private void processCustomMessage(Context context, Bundle bundle) { NotificationCompat.Builder notification = new NotificationCompat.Builder(context); //這一步必須要有而且setSmallIcon也必須要,沒有就會設定自定義聲音不成功 notification.setAutoCancel(true).setSmallIcon(R.mipmap.logo1); String alert = bundle.getString(JPushInterface.EXTRA_ALERT); if (alert!=null&&!alert.equals("")){ notification.setSound( Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.notification)); } //最後重新整理notification是必須的 NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1,notification.build()); }
另外三個ExampleUtil, Logger,TagAliasOperatorHelper為輔助類極光官方提供。
做完這些之後推送還是不能推送到你的手機,因為你沒有把你的裝置號給到後臺,所以登陸的是要做處理
獲取本地裝置號然後請求極光提供的方法setJupshAlisa方法
//獲取手機裝置號 TelephonyManager tm = (TelephonyManager) LoginActivity.this.getSystemService(Context.TELEPHONY_SERVICE); String deviceId = tm.getDeviceId(); SetJuPshAlias(deviceId);
/**
* 向極光繫結別名(後臺必須要根據註冊在極光的別名進行個推)
*/
/**
* 設定、覆蓋簽名(可以自定邏輯判斷(如果設定過了就不需要在設定)
* <p>
* String Myalias:別名(和後臺商定(例如使用Uid))
*/
public static final int ACTION_SET = 2;
public void SetJuPshAlias(String Myalias) {
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
tagAliasBean.isAliasAction = true;
tagAliasBean.action = ACTION_SET;
tagAliasBean.alias = Myalias;
//TagAliasOperatorHelper.getInstance().handleAction(getActivity(),sequence,tagAliasBean);
JPushInterface.setAlias(this, sequence, tagAliasBean.alias);
regId = JPushInterface.getRegistrationID(this);
Log.e("tag", "極光裝置號:"+regId);
if (regId!=null&&!regId.equals("")){
btnlogin.setOnClickListener(this);
}else {
showToast("裝置號為空,請重新登入");
userName.setText("");
passward.setText("");
}
}
得到極光返回的裝置號之後就可以登入啦