首页 > 解决方案 > 从通知启动活动时显示空白白屏

问题描述

我正在使用 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())
}

}

标签: androidfirebase-cloud-messagingandroid-notificationsandroid-pendingintentpusher

解决方案


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

正是从启动屏幕活动的onCreate方法中,我从意图的额外内容中获取通知数据并使用它,就像我收到来自的通知一样NotificationsMessagingService

class SplashActivity : Activity(){


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

推荐阅读