通知栏声音的开启与关闭

问题引出:在项目中牵涉到设置,里面有对推送信息的声音的设置。

思路:1、开始觉得既然是用

notification.defaults = Notification.DEFAULT_SOUND;

来开启声音,那么关闭就应该用类似

notification.defaults = close;

这样的方式,但是查到的资料发现没有这么用的。

所以,舍弃这种方式。

2、因为牵涉到推送的功能,所以难免要看第三方推送给出的demo,这里使用的是极光推送。里面对于通知栏的设置是写在一个方法里面的,代码如下:

<span style="white-space:pre">	</span>/**
	 *设置通知提示方式 - 基础属性
	 */
	private void setStyleBasic(){
		BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(PushSetActivity.this);
		builder.statusBarDrawable = R.drawable.ic_launcher;
		builder.notificationFlags = Notification.FLAG_AUTO_CANCEL;  //设置为点击后自动消失
		builder.notificationDefaults = Notification.DEFAULT_SOUND;  //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)  
		JPushInterface.setPushNotificationBuilder(1, builder);
		Toast.makeText(PushSetActivity.this, "Basic Builder - 1", Toast.LENGTH_SHORT).show();
	}
每次设置,都会调用这个方法,那么根据这个demo,关闭声音的实现应该是将
builder.notificationDefaults = Notification.DEFAULT_SOUND; 
这句注释掉。

后记:不知道这样做是不是最优解

---------------------------------------后续分割线-----2015-09-09--2015-09-10--------------------------------------------

验证:第2种方式是可行的。示例如下

private void openVoice(boolean opened) {
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification.Builder(AboutActivity.this)
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.app)
                .setContentTitle("标题")
                .setContentText("内容")
                .getNotification();

        if (opened) {   //开启声音
            notification.defaults = Notification.DEFAULT_SOUND;
        }

        notificationManager.notify(1,notification);

    }
但是,使用极光推送的

BasicPushNotificationBuilder
类却不可以。查询极光源码,发现BasicPushNotificationBuilder中有
var1.defaults = this.notificationDefaults;

public int notificationDefaults = -1;
-1,在Notification中对应静态字段 DEFAULT_ALL。所以极光推送中默认通知栏会带有声音、震动。

修改:最终使用极光推送的通知栏设置声音的开关,代码如下

private void setStyleBasic(boolean opened){
		BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(activity);
		builder.statusBarDrawable = R.mipmap.app;
		builder.notificationFlags = Notification.FLAG_AUTO_CANCEL;  //设置为点击后自动消失
		if (opened) {
			builder.notificationDefaults = Notification.DEFAULT_SOUND;  //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)
		} else {
			builder.notificationDefaults = Notification.DEFAULT_LIGHTS;	//设置为闪光
		}

		JPushInterface.setDefaultPushNotificationBuilder(builder);
	}
疑惑:设置为DEFAULT_SOUND时,会有响铃和震动;设置为DEFAULT_LIGHTS时,什么都没有;闪光灯从来都没有过,不知道为什么



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值