android - 从通知启动活动时显示空白白屏
问题描述
我正在使用 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())
}
}
解决方案
当应用程序在后台或被杀死时,NotificationsMessagingService
永远不会被调用。pusher 将通知直接发送到通知抽屉。单击它后,将进入应用程序启动器活动,在我的情况下恰好是启动屏幕活动。
正是从启动屏幕活动的onCreate
方法中,我从意图的额外内容中获取通知数据并使用它,就像我收到来自的通知一样NotificationsMessagingService
class SplashActivity : Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.extras != null) {
processIntent(intent)
}
}
推荐阅读
- c# - Newtonsoft JArray - 从节点指定的数组中选择多个值
- windows - ghc 找不到我使用 cabal new-install --lib 构建和安装的库
- python - 验证用户输入中的所有字符都是预先批准的字符列表的一部分
- php - 如何在刀片(Laravel)的前一天之前检查第二天是否可用
- string - Ocaml String.sub:如何获取所有字符直到字符串结束
- identityserver4 - Identity Server 4 和 BlazorWA X-Frame-Options
- visual-studio-extensions - vsts 扩展 + 构建结果增强
- swiftui - 如何在 SwiftUI (iOS14) 中将可点击区域扩展到整个框架?
- c# - 是否可以从对象的路径中检索变量?
- javascript - 语法错误:使用 for-in-loop JavaScript 评估对象中的键时,赋值左侧无效