android 极光推送 自定义声音

版权声明:本文为博主原创文章,未经博主允许不得转载、不得商业用途。 https://blog.csdn.net/chenlove1/article/details/79579785

官方文档未写如何自定义声音https://docs.jiguang.cn/jpush/client/Android/android_sdk/

但可以看到 客户端设置通知栏样式

可以定义样式,那么可以禁用极光的声音,收到通知就播放一个声音就好:

禁用:

		// 极光
		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自定义声音比较简单:

极光后台填入声音文件名(后台代码调用多一个参数而已),然后把声音文件放在工程里即可



要实现不同的通知对不同的声音,安卓要自定义字段解决。

扫描二维码关注公众号,回复: 5701557 查看本文章

猜你喜欢

转载自blog.csdn.net/chenlove1/article/details/79579785
今日推荐