短信通知振动覆盖自定义通知振动

时间:2021-01-28 14:02:25

标签: android notifications android-vibration

我创建了一个拦截短信并解析文本内容的应用。

如果 SMS 文本包含一些定义的字符,应用会显示一条通知,包括声音和自定义振动模式。

if (Build.VERSION.SDK_INT > 25) {
                NotificationChannel channel = new NotificationChannel(channelID,MyApp.APPNAME,NotificationManager.IMPORTANCE_HIGH);
                channel.setVibrationPattern(MyApp.notificationPattern);
                channel.enableVibration(true);
                notificationManager.createNotificationChannel(channel);
            }

        Notification notification = new NotificationCompat.Builder(context,String.valueOf(smsNotify.getID()))
                .setAutoCancel(false)
                .setContentTitle(smsNotify.getTitle())
                .setContentText(smsNotify.getMessage())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                .setChannelId(channelID)
                .setVibrate(new long[]{0,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000})
                .build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;

        notificationManager.notify(idNotification,notification);

有没有办法避免这个问题?

这是模式:

new long[]{0,1000,500,1000,500,1000,500
            ,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500,1000,500
            ,1000,500,1000,500,1000,500,1000,500,1000,500,1000};

当收到短信时,设备按照短信应用的标准模式振动,而我的自定义模式被忽略。如果我关闭短信通知,它运行良好。

我还尝试创建一个使用 Vibrator 的服务:

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
            vibrator.vibrate(VibrationEffect.createOneShot(MyApp.vibrationPatter,n));
        }else{
            vibrator.vibrate(MyApp.vibrationPatter,n);}

结果是一样的。有没有办法避免这种行为?

已解决

我更改了通知渠道,如下所示

if (Build.VERSION.SDK_INT > 25) {
                NotificationChannel channel = new NotificationChannel(channelID,MyApp.APPNAME,NotificationManager.IMPORTANCE_HIGH);
                channel.setVibrationPattern(MyApp.notificationPattern);
                channel.enableVibration(true);
                channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
                channel.setBypassDnd(true);
                notificationManager.createNotificationChannel(channel);
            }

并在通知中添加了一个标志

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_INSISTENT;
        

0 个答案:

没有答案