简体   繁体   English

Android 通知振动不起作用

[英]Android notification vibration not working

I am trying to set notification but something gone wrong when I set vibration for my notification.我正在尝试设置通知,但是当我为通知设置振动时出了点问题。 Even though it still shows notification there is no vibration.即使它仍然显示通知,也没有振动。 I have already added user-permission, android.permission.VIBRATE in manifest here is the code,我已经在清单中添加了用户权限,android.permission.VIBRATE 是代码,

class MainActivity : AppCompatActivity() {

lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
val channelId = "com.example.notificationtest"
val descr = "My notification"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    init()
}

fun init() {

    val show = findViewById<Button>(R.id.show)
    notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    show.setOnClickListener {

        val intent = Intent(applicationContext, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel =
                NotificationChannel(channelId, descr, NotificationManager.IMPORTANCE_HIGH)
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.RED
            notificationChannel.enableVibration(true)
            notificationManager.createNotificationChannel(notificationChannel)

            builder = Notification.Builder(this, channelId)
                .setContentTitle("Android")
                .setContentText("new message")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
        }
        notificationManager.notify(0, builder.build())
    }
}}

Please use the following code snippet for Android Notification with Vibration setup.请使用以下代码片段进行振动设置的 Android 通知。

NotificationManager notificationManager;
NotificationCompat.Builder mBuilder = null;
String channelId = "com.example.notificationtest";
String descr = "My notification";
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, descr, importance);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);

            mBuilder = buildNotification(this, "new Message", "Android", null, R.drawable.home, channelId);

            mBuilder.setChannelId(channelId);
        } else {
            mBuilder = new NotificationCompat.Builder(this, channelId);
            mBuilder.setSmallIcon(R.drawable.home);
            mBuilder.setContentTitle("Android")
                    .setContentText("new Message")
                    .setAutoCancel(false)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        }
        notificationManager.notify(0, mBuilder.build());

And create a new method like,并创建一个新方法,例如,

 @TargetApi(Build.VERSION_CODES.O)
    public NotificationCompat.Builder buildNotification(Context mContext, String text, String fromnumber, PendingIntent pendingIntent, int icon, String channelId) {
        return new NotificationCompat.Builder(mContext, channelId)
                .setSmallIcon(icon)
                .setContentTitle(fromnumber)
                .setSubText(mContext.getString(R.string.app_name))
                .setContentText(text)
                .setAutoCancel(true)
                .setOngoing(true)
                .setAutoCancel(true);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM