【发布时间】:2021-11-01 09:29:33
【问题描述】:
我正在使用 Pusher(依赖于 Firebase Cloud 消息传递)向我的 android 应用发送通知。当应用程序在前台时,我单击通知抽屉中收到的通知,main 活动将启动而没有任何问题。
当我通过长按返回按钮关闭应用程序时,我仍然会收到通知,但是当我单击它时,会打开一个空白的白色屏幕并且应用程序仍然停留在此页面上。将日志添加到我的活动onCreate 显示它从未在这种情况下调用过。
我什至尝试在我的待定意图中设置下面的标志,但它们没有工作
intent.apply {
flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
}
即使在清单文件中的活动声明中添加android:launchMode="singleTask" 也不能解决问题。
清单
<activity
android:name=".find.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar.TransparentStatus"
android:windowSoftInputMode="adjustNothing|stateAlwaysHidden"
/>
消息服务
class NotificationsMessagingService : MessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.e("MessagingService", " NOTIFICATION RECEIVED")
var intent = Intent(this, MainActivity::class.java)
intent.apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
sendNotification("You have a new notification",remoteMessage.notification.body, intent)
}
fun sendNotification(title: String, body: String, intent: Intent) {
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val channelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, getString(R.string.notification_title), NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, notificationBuilder.build())
}
}
【问题讨论】:
-
我很好奇当您将此
<item name="android:windowBackground">@android:color/black</item>添加到您的主题时会发生什么。现在屏幕是黑的而不是白的吗? -
@avalerio 是的,这将显示一秒钟的白屏,然后显示黑屏。主要问题是屏幕是空白的。主要活动永远不会被调用
-
请在创建
0时将PendingIntent.FLAG_ONE_SHOT替换为PendingIntent,看看是否有帮助。 -
另外请发
MainActivity.onCreate()的代码。另外,发生这种情况时您检查过logcat吗?请不要过滤 logcat,否则您可能会错过一些重要的东西。您可能会看到应用程序未正确启动的原因。我很难相信onCreate()没有被调用,因为那会显示白屏。 -
@DavidWasser 在下面查看我的答案。我知道发生了什么
标签: android firebase-cloud-messaging android-notifications android-pendingintent pusher