java - Android 通知振动不起作用

标签 java android kotlin notifications

我正在尝试设置通知,但当我为通知设置振动时出现问题。 即使它仍然显示通知,但没有振动。 我已经在 list 中添加了用户权限 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())
    }
}}

最佳答案

请使用以下代码 fragment 进行带有振动设置的 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());

并创建一个新方法,例如,

 @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);
    }

关于java - Android 通知振动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60734761/

相关文章:

java - 如何检索在服务器 (Tomcat) 上上传的文件的正确 URL(使用 spring MVC)

java - 如何将字符串值数组从 Controller 传递到 View

javascript - Kotlin JS 项目的基本 Gradle 脚本 Kotlin 文件是什么样的?

java - EditText 没有过滤 Android Kotlin 中的 RecyclerView 列表

java - 每 x 分钟检查一次测试状态的最佳方法是什么?

java - 在java中实例化自界泛型

android - Localytics - 以编程方式设置应用程序 key

android - 具有运行时权限的基于 gps 的应用程序的体系结构

具有多项选择的 Android Spinner

kotlin - 将选项列表映射到字符串列表