我正在实现FCM通知功能,我从后端收到了一条通知,但是它没有包含任何数据,只有一个标题。这意味着,如果当此通知到达时我的应用程序没有运行,并且用户打开此通知,则我的应用程序将被启动,并且在其意图中附加的“extras”和“data”为空。
是否有一种方法可以区分这个“空通知”启动和正常的“用户点击应用程序图标”启动应用程序的方式,以便在需要时将用户带到应用程序的“通知”部分?
这两种情况下,它们的意图中都没有数据和额外信息。
我正在实现FCM通知功能,我从后端收到了一条通知,但是它没有包含任何数据,只有一个标题。这意味着,如果当此通知到达时我的应用程序没有运行,并且用户打开此通知,则我的应用程序将被启动,并且在其意图中附加的“extras”和“data”为空。
是否有一种方法可以区分这个“空通知”启动和正常的“用户点击应用程序图标”启动应用程序的方式,以便在需要时将用户带到应用程序的“通知”部分?
这两种情况下,它们的意图中都没有数据和额外信息。
在创建启动应用程序的Intent时,您需要使用putExtra(ID_KEY, id)
方法,并且在您的onCreate()
方法中,您可以使用getIntent().getExtras().getInt(ID_KEY)
来检索传递的id。
是的,你可以。你可以为每个Intent
指定它的action
。就像这样:
private fun sendNotification(title: String, message: String) {
// Firstly, you create an intent that will open an activity when user taps on your notification.
val intent = Intent(this, ProfileScreenActivity::class.java)
.putExtra(INTENT_EXTRA_FIREBASE_MESSAGE_TITLE, title) // Add Title
.putExtra(INTENT_EXTRA_FIREBASE_MESSAGE_BODY, message) // Add Message
// Optional, but it basically clears backstack and all hierarchy of open Fragments/Activities
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
// Here is where we specify that this Intent comes from Firebase Service
intent.action = INTENT_ACTION_FIREBASE_MESSAGE_DIALOG
// Wrap all of this
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
// Standard code for creating and sending Notifications.
val channelId = getString(R.string.firebase_default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_MAX)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, DEFAULT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Random.nextInt(0, 100), notificationBuilder.build())
}
然后,在用户点击通知时打开的Activity中,您需要执行以下操作:
// Check if any actions were passed to this Activity when it's started.
// If any actions were passed, we check what action was passed and proceed from there.
intent.action?.let { action ->
if (action == INTENT_ACTION_FIREBASE_MESSAGE_DIALOG) {
intent.extras?.let { extras ->
alertDialog.warning(this, extras.getString(INTENT_EXTRA_FIREBASE_MESSAGE_BODY), extras.getString(INTENT_EXTRA_FIREBASE_MESSAGE_TITLE))
}
}
if (action == INTENT_ACTION_INTERNET_DAYS_LEFT_DIALOG) {
intent.extras?.let { extras ->
alertDialog.warning(this, extras.getString(INTENT_EXTRA_INTERNET_DAYS_LEFT))
}
}
}
FirebaseMessagingService
处理的通知引起的,而是在我的应用程序和服务未运行时到达的通知,当用户点击通知时通过意图传递给应用程序。这就是我试图区分的情况。 - undefinedFirebaseMessagingService
处理,所以只需使用我提供的代码,您就能实现您所需的功能。 - undefined