我正在使用Firebase云消息在android应用程序中实现通知。我是安卓系统的新手。请帮帮我。
问题
当我在前台状态下收到通知时,出现了意外的移动。
- 期望:在收到通知时什么都不做。我想在点击通知时处理它
- 实际:BroadcastReceiver的onReceive在收到通知时调用。当我点击它时,它没有响应(
"test onCreate"
没有显示在logcat中(
注意:此应用程序完全由MainActivity上的一个片段操作
文件
PushNotificationListenerService.kt
class PushNotificationListenerService: FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val channelId = message.notification?.channelId.toString()
val title: String = message.notification?.title.toString()
val text: String = message.notification?.body.toString()
sendNotification(
title,
text,
channelId
)
var broadcast = Intent()
broadcast.action = "FCM_RECEIVE_FOREGROUND"
sendBroadcast(broadcast)
}
private fun sendNotification(
title: String,
text: String,
receivedChannelId: String,
) {
val intent = Intent(this, HomeFragment::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
.apply {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val notificationBuilder = NotificationCompat.Builder(this, receivedChannelId)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_menu_logo)
.setContentIntent(pendingIntent)
.setPriority(PRIORITY_MAX)
.setCategory(CATEGORY_CALL)
.setAutoCancel(true)
.setSound(null)
.setVibrate(null)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val uuid = UUID.randomUUID().hashCode()
notificationManager.notify(uuid, notificationBuilder.build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(receivedChannelId)
}
}
}
主要活动.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
private var mReceiver: BroadcastReceiver? = null
private val mIntentFilter = IntentFilter("FCM_RECEIVE_FOREGROUND")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("test onCreate")
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
mReceiver = (object: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
Timber.d("test onReceive called")
if (p1 == null) { return }
}
})
setupChannels()
}
override fun onResume() {
super.onResume()
registerReceiver(mReceiver, mIntentFilter)
}
override fun onPause() {
if (mReceiver != null) {
unregisterReceiver(mReceiver)
mReceiver = null
}
super.onPause()
}
private fun setupChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val attribute = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val channel = NotificationChannel(
"channelId",
"channelName",
NotificationManager.IMPORTANCE_HIGH
).apply {
vibrationPattern = createVibrate()
lightColor = Color.BLUE
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
setSound(uri, attribute)
}
manager.createNotificationChannel(channel)
}
}
}
Firebase云消息通知的有效负载
const fcmTokenMessage: admin.messaging.TokenMessage = {
android: {
data: {},
notification: {
body: "message",
title: "title",
defaultSound: true,
channelId: "channelId",
sound: 'default',
priority: 'high',
notificationCount: fcmPushData.notificationCount
}
},
token: fcmPushData.fcmPushToken
};
版本
build.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
classpath "org.jetbrains.kotlin:kotlin-serialization:1.4.32"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath 'com.google.gms:google-services:4.3.8'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
仅此而已。对于给您带来的不便,我深表歉意,并感谢您的合作。
已解决。问题是Fragment被指定为Intent,当指定了Activity时,它就工作了。