问题引出:在项目中牵涉到设置,里面有对推送信息的声音的设置。
思路: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时,什么都没有;闪光灯从来都没有过,不知道为什么