【问题标题】:Blank white screen shown when starting an activity from a notification从通知启动活动时显示空白白屏
【发布时间】: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())
}

}

【问题讨论】:

  • 我很好奇当您将此&lt;item name="android:windowBackground"&gt;@android:color/black&lt;/item&gt; 添加到您的主题时会发生什么。现在屏幕是黑的而不是白的吗?
  • @avalerio 是的,这将显示一秒钟的白屏,然后显示黑屏。主要问题是屏幕是空白的。主要活动永远不会被调用
  • 请在创建0 时将PendingIntent.FLAG_ONE_SHOT 替换为PendingIntent,看看是否有帮助。
  • 另外请发MainActivity.onCreate()的代码。另外,发生这种情况时您检查过logcat吗?请不要过滤 logcat,否则您可能会错过一些重要的东西。您可能会看到应用程序未正确启动的原因。我很难相信 onCreate() 没有被调用,因为那会显示白屏。
  • @DavidWasser 在下面查看我的答案。我知道发生了什么

标签: android firebase-cloud-messaging android-notifications android-pendingintent pusher


【解决方案1】:

当应用程序在后台或被杀死时,NotificationsMessagingService 永远不会被调用。 pusher 将通知直接发送到通知抽屉。单击它后,将进入应用启动器活动,在我的情况下恰好是启动屏幕活动。

我从初始屏幕活动的onCreate 方法中获取通知数据,并使用它,就像我收到来自NotificationsMessagingService 的通知时一样

class SplashActivity : Activity(){


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   
    if (intent.extras != null) {
        processIntent(intent)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多