前言
最近的項目需要自定義推送聲音,然而極光的文檔,說得模凌兩可的,弄來弄去整了一天才搞好這個自定義推送聲音,因此記錄一下。
實現思路
極光推送包含有通知與自定義消息兩種類型的推送。因此可以發送三種方式的推送:
- 通知
- 自定義消息
- 通知 + 自定義消息
要實現自定義推送聲音,需要使用第二種方式。
代碼實現
使用自定義消息,在客戶端App里需要接受 JPush SDK 的廣播:
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));
processCustomMessage(context, bundle);
} 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] 用戶點擊打開了通知");
//打開自定義的Activity
Intent i = new Intent(context, TestActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
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());
}
}
/**
* 實現自定義推送聲音
* @param context
* @param bundle
*/
private void processCustomMessage(Context context, Bundle bundle) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
notification.setAutoCancel(true)
.setContentText("自定義推送聲音")
.setContentTitle("極光測試")
.setSmallIcon(R.mipmap.ic_launcher);
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
if (!TextUtils.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (null != extraJson && extraJson.length() > 0) {
String sound = extraJson.getString("sound");
if("test.mp3".equals(sound)){
notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));
}
}
} catch (JSONException e) {
}
}
Intent mIntent = new Intent(context,TestActivity.class);
mIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
notification.setContentIntent(pendingIntent);
notificationManager.notify(2, notification.build());
}
}
測試
打開jpush控制臺,模擬發送自定義消息。如圖:
jpush01.png
選擇可選設置,填寫附加字段。如圖:
jpush02.png
這里的test.mp3,我保存在raw文件下,如圖:
jpush03.png
Jpush自定義推送聲音demo
項目中的賬號,是個人申請的,如果需要賬號測試,可以聯系我。