Ada*_*gyi 6 android android-notifications
我在显示通知时尝试禁用振动.
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;
}
Run Code Online (Sandbox Code Playgroud)
我在一个活动的onCreate()上调用它,如下所示:
Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)
它还在振动.我在Android 8设备上测试.我也试过了
notificationChannel.setVibrationPattern(null);
Run Code Online (Sandbox Code Playgroud)
仍然没有用.
我有
<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)
无论我如何定义振动模式,如:
new long[]{1000L, 500L, 300L, 1000L};
Run Code Online (Sandbox Code Playgroud)
振动与我的设置不符.Onyl默认发生"两短"振动.
如果可以,请提供帮助,提前谢谢.
编辑:
正如Avijit Karmakar所说,我补充道
notificationChannel.enableVibration(false);
Run Code Online (Sandbox Code Playgroud)
完整代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在小米米A1(Android 8.0)上测试
任何人都可以尝试这个代码并帮助我获得结果吗?
Mit*_*oto 11
使用NotificationManager.IMPORTANCE_LOW作为重要性值。
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
);
Run Code Online (Sandbox Code Playgroud)
pcs*_*ana 10
喜欢这个答案,做:
mNotificationChannel.setVibrationPattern(new long[]{ 0 });
mNotificationChannel.enableVibration(true);
Run Code Online (Sandbox Code Playgroud)
重要事项1:即使我设置了上面的振动模式,但将enableVibration设置为false,它也会振动.所以,将enableVibration设置为true!
重要2:像另一个答案一样,频道会保留其初始设置,因此请再次卸载并安装应用以应用更改!
希望能帮助到你!
将此行添加到您的代码中以停止振动:
notificationChannel.enableVibration(false);
// Above line will disable your vibration for the notification
Run Code Online (Sandbox Code Playgroud)
另外,删除振动模式。
因此,您更新的代码将是:
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});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6858 次 |
| 最近记录: |