1. 程式人生 > >android 極光推送 自定義聲音

android 極光推送 自定義聲音

可以定義樣式,那麼可以禁用極光的聲音,收到通知就播放一個聲音就好:

禁用:

		// 極光
		JPushInterface.setDebugMode(IS_DEBUG);
		JPushInterface.init(this);
		BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
		builder.statusBarDrawable = R.drawable.icon_apk_fangzhi;
		builder.notificationFlags = Notification.FLAG_AUTO_CANCEL//
				| Notification.FLAG_SHOW_LIGHTS; // 設定為自動消失和呼吸燈閃爍
		builder.notificationDefaults = //
//				Notification.DEFAULT_SOUND | // 設定為鈴聲
						Notification.DEFAULT_VIBRATE | // 設定為、震動
						Notification.DEFAULT_LIGHTS; // 設定為呼吸燈閃爍
		JPushInterface.setPushNotificationBuilder(1, builder);

在廣播裡播放SoundHelper.get().palyOrder();:

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		if (null == bundle) {
			return;
		}
		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())) {
			SoundHelper.get().palyOrder();

SoundHelper類:

package com.--.comm.helper;


import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.view.View;

//SoundPool.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
//引數依次是: 
//soundID:Load()返回的聲音ID號 
//leftVolume:左聲道音量設定 
//rightVolume:右聲道音量設定 
//priority:指定播放聲音的優先順序,數值越高,優先順序越大。 
//loop:指定是否迴圈:-1表示無限迴圈,0表示不迴圈,其他值表示要重複播放的次數 
//rate:指定播放速率:1.0的播放率可以使聲音按照其原始頻率,而2.0的播放速率,可以使聲音按照其 
//原始頻率的兩倍播放。如果為0.5的播放率,則播放速率是原始頻率的一半。播放速率的取值範圍是0.5至2.0。
public class SoundHelper {

	private SoundPool soundPool;
	private int idSure;
	private int idCanel;
	private int idOrder;

	private static SoundHelper helper;

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public SoundHelper() {
		Context context = Application.mContext;

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			SoundPool.Builder spb = new SoundPool.Builder();
			spb.setMaxStreams(10);
			// spb.setAudioAttributes(null); // 轉換音訊格式
			soundPool = spb.build(); // 建立SoundPool物件

		} else {
			soundPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
		}
		idSure = soundPool.load(context, R.raw.h_sure_open, 1);
		idCanel = soundPool.load(context, R.raw.h_cancel_back, 1);
		idOrder = soundPool.load(context, R.raw.order, 1);
	}

	public static SoundHelper get() {
		if (null == helper) {
			helper = new SoundHelper();
		}
		return helper;
	}

	public void palyOrder() {
			soundPool.play(idOrder, 1, 1, 10, 0, 1);
		
	}
	
	public void palySure() {
		if (BaseApplication.get().isOpenSoundBtn() && DefaultShared.isH()) {
			soundPool.play(idSure, 1, 1, 0, 0, 1);
		}

	}

	public void palyCancel() {
		if (BaseApplication.get().isOpenSoundBtn() && DefaultShared.isH()) {
			soundPool.play(idCanel, 1, 1, 0, 0, 1);
		}
	}

}

IOS自定義聲音比較簡單:

極光後臺填入聲音檔名(後臺程式碼呼叫多一個引數而已),然後把聲音檔案放在工程裡即可


要實現不同的通知對不同的聲音,安卓要自定義欄位解決。