【发布时间】:2018-12-12 08:38:39
【问题描述】:
单击通知时打开应用程序时,我试图从捆绑包中获取额外信息。
我在 MainActivity 上有这个方法:
private void getBundleFromFirebaseMessaging(){
Intent intent = this.getIntent();
if (intent != null && intent.getExtras() != null) {
Bundle b = getIntent().getExtras();
boolean cameFromNotification = b.getBoolean("fromNotification");
if (cameFromNotification) {
Toast.makeText(this, "FROM PUSH", Toast.LENGTH_SHORT).show();
}
}
我得到的唯一捆绑包是 _fbSourceApplicationHasBeenSet。
这是我在 FirebaseMessaging 类中的方法
private fun sendNotification(message: RemoteMessage) {
val messageBody = message.notification?.body
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fromNotification", true)
val pendingIntent = PendingIntent.getActivity(this,
0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = getString(R.string.notification_channel)
val channelName = getString(R.string.notification_channel_name)
val defaultSoundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat
.Builder(this, channelId)
.setSmallIcon(R.drawable.login_logo)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
【问题讨论】:
-
经历同样的事情。你解决了那个问题吗?出了什么问题?
-
经过一番调查,我发现我的问题与 _fbSourceApplicationHasBeenSet 无关 在我的情况下,我只是从错误的活动中读取了意图。
标签: android firebase push-notification kotlin firebase-cloud-messaging