Android 11 FCM 锁定屏幕问题(Kotlin)

发布于 2025-01-20 13:47:36 字数 3096 浏览 2 评论 0 原文

我有一个申请。我扩展了Firebasemessagingservie。我将其添加到清单中:

        <service
        android:name=".services.AutomentoFirebaseMessagingService"
        android:directBootAware="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

我还过度

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
    Log.d(TAG, "Topic message: " + remoteMessage.from)
    super.onMessageReceived(remoteMessage)

    val powerIntent = Intent(Intent.ACTION_POWER_CONNECTED)
    context?.sendBroadcast(powerIntent)

    wakeUpScreen()

    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        this,
        REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT
    )
    val channelId = getString(R.string.default_notification_channel_id)
    val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.ic_rendeleseim_white_48)
        .setContentTitle(remoteMessage.notification!!.title)
        .setContentText(remoteMessage.notification!!.body)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .setOngoing(true)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setLights(Color.RED, 500, 500)
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    val channel = NotificationChannel(
        channelId,
        "Pushnotification",
        NotificationManager.IMPORTANCE_HIGH,
    ).apply {
        lockscreenVisibility = Notification.VISIBILITY_PUBLIC
    }
    
    notificationManager.createNotificationChannel(channel)
    val notification = notificationBuilder.build()
    notification.flags = notification.flags or Notification.FLAG_INSISTENT
    notificationManager.notify(REQUEST_CODE, notification)
}

private fun wakeUpScreen() {
    val powerManager = getSystemService(POWER_SERVICE) as PowerManager
    val isScreenOn: Boolean = powerManager.isInteractive
    if (!isScreenOn) {
        val wakeLock: PowerManager.WakeLock =
            (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MentoMano::WakelockTag").apply {
                    acquire(1000)
                }
            }
    }

    val pm = context!!.getSystemService(POWER_SERVICE) as PowerManager
    val isScreenOn2 = pm.isInteractive // check if screen is on

    if (!isScreenOn2) {
        val wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock")
        wl.acquire(3000) //set your time in milliseconds
    }
}

使用了API密钥,并且获得了令牌。我可以发送通知,并且当应用程序运行时,用户在后台且未运行时将其获取。

我的问题:当屏幕锁定时,用户无法收到通知。我配置了手机。当手机锁定时,我可以从Facebook Messenger获得pushnotification,但我的 Notification 到达 当用户打开屏幕或屏幕是时打开。

I have an application. I extended the FirebaseMessagingServie. I added it into the manifest:

        <service
        android:name=".services.AutomentoFirebaseMessagingService"
        android:directBootAware="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

I also overided the onMessageRecived method

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
    Log.d(TAG, "Topic message: " + remoteMessage.from)
    super.onMessageReceived(remoteMessage)

    val powerIntent = Intent(Intent.ACTION_POWER_CONNECTED)
    context?.sendBroadcast(powerIntent)

    wakeUpScreen()

    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        this,
        REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT
    )
    val channelId = getString(R.string.default_notification_channel_id)
    val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.ic_rendeleseim_white_48)
        .setContentTitle(remoteMessage.notification!!.title)
        .setContentText(remoteMessage.notification!!.body)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .setOngoing(true)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setLights(Color.RED, 500, 500)
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    val channel = NotificationChannel(
        channelId,
        "Pushnotification",
        NotificationManager.IMPORTANCE_HIGH,
    ).apply {
        lockscreenVisibility = Notification.VISIBILITY_PUBLIC
    }
    
    notificationManager.createNotificationChannel(channel)
    val notification = notificationBuilder.build()
    notification.flags = notification.flags or Notification.FLAG_INSISTENT
    notificationManager.notify(REQUEST_CODE, notification)
}

private fun wakeUpScreen() {
    val powerManager = getSystemService(POWER_SERVICE) as PowerManager
    val isScreenOn: Boolean = powerManager.isInteractive
    if (!isScreenOn) {
        val wakeLock: PowerManager.WakeLock =
            (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MentoMano::WakelockTag").apply {
                    acquire(1000)
                }
            }
    }

    val pm = context!!.getSystemService(POWER_SERVICE) as PowerManager
    val isScreenOn2 = pm.isInteractive // check if screen is on

    if (!isScreenOn2) {
        val wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock")
        wl.acquire(3000) //set your time in milliseconds
    }
}

I have the API key and I get the token. I can send a notification and the user get it when the app is runing, when it is in the background and it is not running.

My problem: the user doesn't get the notification when the screen is locked. I configured the phone. I can get pushnotification from the facebook Messenger when the phone is locked, but my notification arrives only when the user opens the screen or the screen is opened.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2025-01-27 13:47:36

如果它有助于某种方法,我使用了此解决方案。

我保留了清单,就像

从Firebasemessagingservice继承的

class MyMessagingService: FirebaseMessagingService {

问题中,我使用了此onMessagerecibed函数,

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
        Log.d(TAG, "Topic message: " + remoteMessage.from)
        super.onMessageReceived(remoteMessage)

        val powerIntent = Intent(Intent.ACTION_POWER_CONNECTED)
        context?.sendBroadcast(powerIntent)

        wakeUpScreen()

        val intent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_IMMUTABLE
        )
        val channelId = getString(R.string.default_notification_channel_id)
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_rendeleseim_white_48)
            .setContentTitle(remoteMessage.notification!!.title)
            .setContentText(remoteMessage.notification!!.body)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setLights(Color.RED, 500, 500)
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val channel = NotificationChannel(
            channelId,
            "Pushnotification",
            NotificationManager.IMPORTANCE_HIGH,
        ).apply {
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
        }

        notificationManager.createNotificationChannel(channel)
        val notification = notificationBuilder.build()
        notification.flags = notification.flags or Notification.FLAG_INSISTENT
        notificationManager.notify(REQUEST_CODE, notification)
    }

这是唤醒屏幕方法

  private fun wakeUpScreen() {
        val powerManager = getSystemService(POWER_SERVICE) as PowerManager
        val isScreenOn: Boolean = powerManager.isInteractive
        if (!isScreenOn) {
            val wakeLock: PowerManager.WakeLock =
                (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                    newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MentoMano::WakelockTag").apply {
                        acquire(1000)
                    }
                }
        }

        val pm = baseContext.getSystemService(POWER_SERVICE) as PowerManager
        val isScreenOn2 = pm.isInteractive // check if screen is on

        if (!isScreenOn2) {
            val wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock")
            wl.acquire(3000) //set your time in milliseconds
        }
    }

If it helps somebodey I used this solution.

I kept the manifest as in the question

In inherited from FirebaseMessagingService

class MyMessagingService: FirebaseMessagingService {

And I used this onMessageRecibed function

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
        Log.d(TAG, "Topic message: " + remoteMessage.from)
        super.onMessageReceived(remoteMessage)

        val powerIntent = Intent(Intent.ACTION_POWER_CONNECTED)
        context?.sendBroadcast(powerIntent)

        wakeUpScreen()

        val intent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_IMMUTABLE
        )
        val channelId = getString(R.string.default_notification_channel_id)
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_rendeleseim_white_48)
            .setContentTitle(remoteMessage.notification!!.title)
            .setContentText(remoteMessage.notification!!.body)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setLights(Color.RED, 500, 500)
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val channel = NotificationChannel(
            channelId,
            "Pushnotification",
            NotificationManager.IMPORTANCE_HIGH,
        ).apply {
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
        }

        notificationManager.createNotificationChannel(channel)
        val notification = notificationBuilder.build()
        notification.flags = notification.flags or Notification.FLAG_INSISTENT
        notificationManager.notify(REQUEST_CODE, notification)
    }

This is the wakeUpScreen method

  private fun wakeUpScreen() {
        val powerManager = getSystemService(POWER_SERVICE) as PowerManager
        val isScreenOn: Boolean = powerManager.isInteractive
        if (!isScreenOn) {
            val wakeLock: PowerManager.WakeLock =
                (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
                    newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MentoMano::WakelockTag").apply {
                        acquire(1000)
                    }
                }
        }

        val pm = baseContext.getSystemService(POWER_SERVICE) as PowerManager
        val isScreenOn2 = pm.isInteractive // check if screen is on

        if (!isScreenOn2) {
            val wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock")
            wl.acquire(3000) //set your time in milliseconds
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文