You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android集成Firebase推送通知后设备不振动问题求助

解决方案

核心问题排查与修复

你当前代码存在3个直接导致振动不生效的核心错误,按优先级修复即可:

1. 通知通道创建时机错误

Android 8.0及以上系统中,通知通道必须在任何通知发送前完成创建,你当前的代码逻辑是先发送通知,后创建通道,会导致第一次触发通知时通道不存在,所有通道配置(包括振动)完全不生效。且通知通道一旦创建成功,后续代码修改振动、提示音等参数都不会生效,除非卸载重装应用或更换新的通道ID。
建议将通道创建逻辑提前到Application的onCreate方法,或者推送服务初始化阶段执行。

2. 振动配置冲突

你同时设置了setDefaults(NotificationCompat.DEFAULT_VIBRATE)和自定义setVibrate参数,系统默认配置会直接覆盖你的自定义振动规则,如需使用自定义振动模式,直接删除setDefaults(NotificationCompat.DEFAULT_VIBRATE)这行代码即可。

3. 冗余逻辑干扰

你不需要单独调用Vibrator服务触发振动,通知通道配置正确的情况下系统会自动触发振动。且Android 12(API 31)及以上单独调用振动需要额外申请VIBRATE_MANAGE权限,反而增加异常概率。


修正后可运行代码

const val channelId = "notification_channel"
const val channelName = "com.deskmateai.t2chaiwala"
val vibration = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200)

class MyFirebaseMessagingService: FirebaseMessagingService() {
    override fun onCreate() {
        super.onCreate()
        // 服务初始化时先创建通知通道
        createNotificationChannel()
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        // 替换!!强转避免空指针崩溃
        remoteMessage.notification?.let { notification ->
            generateNotification(notification.title ?: "", notification.body ?: "")
        }
    }

    // 生成通知
    private fun generateNotification(title: String, description: String){
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, channelId)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.tea_notify_logo)
            .setAutoCancel(true)
            .setContentText(description)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setVibrate(vibration)
        
        val manager: NotificationManagerCompat = NotificationManagerCompat.from(applicationContext)
        manager.notify(1, builder.build())
    }

    // 独立的通道创建方法
    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
            channel.enableLights(true)
            channel.enableVibration(true)
            channel.vibrationPattern = vibration
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
        }
    }
}

同时确认你的AndroidManifest.xml中已经添加基础振动权限:

<uses-permission android:name="android.permission.VIBRATE" />

额外验证项

如果修改代码后仍无振动,按以下步骤排查:

  • 先卸载旧版本应用重装,清除之前创建的无效通道缓存
  • 检查设备是否开启了免打扰模式,免打扰模式下会默认禁止通知振动
  • 进入应用的系统通知设置页,检查对应通知通道的振动开关是否被手动关闭

内容的提问来源于stack exchange,提问作者Amit Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:45:04