我有两个文件。一个文件是名为 MyFirebaseMessagingService.kt
```
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val ADMIN_CHANNEL_ID = "admin_channel"
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
val intent = Intent(this, ChatActivity::class.java)
val username = p0.data["title"]
val ref = FirebaseDatabase.getInstance().getReference("/users/").orderByChild("username").equalTo(username).addValueEventListener(object: ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
for(child in snapshot.children){
var user : User? = child.getValue(User::class.java)
if(user != null){
Log.d("TAG", user.username)
Log.d("TAG", user.image_url)
Log.d("TAG", user.uid)
}
intent.putExtra(USER_KEY, user)
}
}
})
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationID = Random().nextInt(3000)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupChannels(notificationManager)
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_ONE_SHOT
)
/*
val largeIcon = BitmapFactory.decodeResource(
resources,
R.drawable.ic_delete
)*/
val notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.custom_user_icon)
//.setLargeIcon(largeIcon)
.setContentTitle(p0?.data?.get("title"))
.setContentText(p0?.data?.get("message"))
.setAutoCancel(true)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent)
//Set notification color to match app color template
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.color = resources.getColor(R.color.black)
}
notificationManager.notify(notificationID, notificationBuilder.build())
}
@RequiresApi(api = Build.VERSION_CODES.O)
private fun setupChannels(notificationManager: NotificationManager?) {
val adminChannelName = "New notification"
val adminChannelDescription = "Device to device notification"
val adminChannel: NotificationChannel
adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH)
adminChannel.description = adminChannelDescription
adminChannel.enableLights(true)
adminChannel.lightColor = Color.GREEN
adminChannel.enableVibration(true)
notificationManager?.createNotificationChannel(adminChannel)
}
}
第二个文件是一个名为 `ChatActivity.kt` ```
class ChatActivity : AppCompatActivity() {
val adapter = GroupAdapter<GroupieViewHolder>()
var receiver : User? = null
private val FCM_API = "https://fcm.googleapis.com/fcm/send"
private val serverKey =
"key=" + "my api key"
private val contentType = "application/json"
private val requestQueue: RequestQueue by lazy {
Volley.newRequestQueue(this.applicationContext)
}
companion object{
var date_object: Message? = null
var sender_name :String? = null
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
val recycler_view = findViewById<RecyclerView>(R.id.recycler_view_messages)
recycler_view.adapter = adapter
receiver = intent.getParcelableExtra<User>(NewMessageActivity.USER_KEY)
supportActionBar?.title = receiver?.username
val ref = FirebaseDatabase.getInstance().getReference("/users/${FirebaseAuth.getInstance().uid}"+"/username").addValueEventListener(object: ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
sender_name = snapshot.getValue(String::class.java)
Log.e("TAG", "Ricevuta stringa "+sender_name)
}})
}
....
}
我使用parcelable类的对象 User
作为数据 putParcelableExtra()
方法,这是用户类。
class User(val uid:String, val username:String, val image_url:String): Parcelable{
constructor(): this("", "", "")
}
问题是第一次按下通知时 getParcelableExtra ()
方法不返回对象,因此 receiver
对象为空。但是,当再次按下新通知时,该方法返回可以使用的对象。这个问题只会在你第一次点击通知时出现,在应用程序被打开之后(而且它还没有出现在后台)。
我怎样才能解决这个问题?
1条答案
按热度按时间4xrmg8kj1#
似乎您在ondatachange回调中使用intent.putextra(user\u key,user)保存用户。发生的情况是,通知在ondatachange回调中的代码执行之前显示,因此尚未在意图中设置任何用户。
要解决此问题,应在intent.putextra(user_key,user)调用之后,在myfirebasemessagingservice类的ondatachange方法中显示通知。
编辑:
一个可能的解决办法是: