Android 11 FCM 锁定屏幕问题(Kotlin)
我有一个申请。我扩展了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 到达 当用户打开屏幕或屏幕是时打开。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它有助于某种方法,我使用了此解决方案。
我保留了清单,就像
从Firebasemessagingservice继承的
问题中,我使用了此onMessagerecibed函数,
这是唤醒屏幕方法
If it helps somebodey I used this solution.
I kept the manifest as in the question
In inherited from FirebaseMessagingService
And I used this onMessageRecibed function
This is the wakeUpScreen method