我在显示通知时尝试禁用振动。
FUNC:
public static Notification buildNotifForUploaderService(Context ctx, String title, String message) {
Notification notification;
NotificationCompat.Builder notificationBuilder;
//If device is Android 8+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//setting pattern to disable vibrating
notificationChannel.setVibrationPattern(new long[]{0L});
notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
} else {
notificationBuilder = new NotificationCompat.Builder(ctx);
notificationBuilder.setVibrate(new long[]{0L});
}
notificationBuilder
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_backup_black_24dp);
notification = notificationBuilder.build();
return notification;
}
我在活动的onCreate()上调用了这个:
Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
仍在振动。我在 Android 8 设备上测试。 我也试过
notificationChannel.setVibrationPattern(null);
仍然不起作用。
我有
<uses-permission android:name="android.permission.VIBRATE" />
无论我如何定义振动模式,例如:
new long[]{1000L, 500L, 300L, 1000L};
振动与我的设置不对应。 Onyl默认&#34;两个短&#34;振动发生。
如果可以,请提供帮助,提前谢谢。
E D I T:
正如Avijit Karmakar所说,我添加了
notificationChannel.enableVibration(false);
完整代码现在:
public class MainActivity extends AppCompatActivity {
final static String CHANNEL_ID = "MY_CHANNEL_ID";
final static String CHANNEL_NAME = "MY_CHANNEL_NAME";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification;
NotificationCompat.Builder mBuilder;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//Disabling vibration!
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
} else {
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setVibrate(new long[]{0L});
}
mBuilder.setContentTitle("title")
.setContentText("message")
.setSmallIcon(R.drawable.ic_android_black_24dp);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mBuilder.setLargeIcon(bm);
notification = mBuilder.build();
notificationManager.notify(1, notification);
}
}
我在Xiaomi Mi A1(Android 8.0)上进行测试
有人可以尝试使用此代码并帮助我查看结果吗?
答案 0 :(得分:7)
喜欢这个answer,请执行以下操作:
mNotificationChannel.setVibrationPattern(new long[]{ 0 });
mNotificationChannel.enableVibration(true);
重要事项1 :即使我在上面设置了振动模式,但将enableVibration设置为false也会振动。因此,请将enableVibration设置为 true !
!重要2 :像another answer一样,频道保留其初始设置,因此卸载并再次安装应用以应用更改!
希望有帮助!
答案 1 :(得分:6)
使用NotificationManager.IMPORTANCE_LOW作为重要性值。
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
);
答案 2 :(得分:1)
将此行添加到代码中以停止振动:
notificationChannel.enableVibration(false);
// Above line will disable your vibration for the notification
另外,去除振动模式。
因此,您更新的代码将是:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//setting pattern to disable vibrating
notificationChannel.enableVibration(false);
notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
} else {
notificationBuilder = new NotificationCompat.Builder(ctx);
notificationBuilder.setVibrate(new long[]{0L});
}
答案 3 :(得分:1)
可以找到Ahmadul Hoq here的答案可能会有所帮助。
基本上你必须启用振动并将振动模式设置为0L。 Android Oreo上似乎存在一个导致此解决方法的错误。
编辑:
如果您使用摘要通知,则可能会导致双重振动。直到我发现与传入通知一起组合的摘要通知导致此问题,我才有相同的行为。您可以为摘要通知创建额外的通知通道,并将此通知的重要性设置为“低”。这意味着摘要通知的频道将保持静音,您应该只有来自正常传入通知的声音和振动。
答案 4 :(得分:1)
此功能对我有用。借助它,我可以在完全禁用灯光,振动和声音的地方创建通知通道。
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
serviceChannel.setVibrationPattern(new long[]{ 0 });
serviceChannel.enableVibration(true);
serviceChannel.enableLights(false);
serviceChannel.setSound(null, null);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}