安卓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>

我还介绍了onMessageRecived方法

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获得推送通知,但我的通知只有在用户打开屏幕或屏幕打开时才到达

如果有帮助的话,我使用了这个解决方案。

我保留了问题中的清单

继承自FirebaseMessagingService

class MyMessagingService: FirebaseMessagingService {

我在MessageRecied函数上使用了这个

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
}
}

相关内容

  • 没有找到相关文章

最新更新