从Firebase收到通知时如何打开其他活动
在我的应用中,有不同的类别,一个主页(MainActivity),评论活动和聊天活动等.当我收到来自评论,聊天或主页的通知时,它仍会打开主要活动.我想要的是,如果我收到有关聊天"的通知,则它应打开聊天活动,如果收到评论,则应打开评论活动,依此类推...请帮助谢谢.
In my app there are different categories, a main page(MainActivity), comments activities and Chat activity etc. When i receive a notification, either from comments, Chat or main page, it still opens main activity. What I want is if I receive a notification regarding Chat it should open chat activity and if regarding comments it should open comments activity and so on... Please help thanks.
您的问题的答案是 PendingIntent
.您将需要在FCM有效负载中发送通知的类型(聊天,评论等),然后在您的 onMessageReceived()
中处理通知,了解通知的类型并使用所需的名称创建PendingIntent活动.当用户点击通知时,Android将启动PendingIntent中指定的活动.请参考 android文档.我希望这会有所帮助.
The answer to your question is PendingIntent
. You will need to send in the FCM payload the type of notification (chat, comments etc.,) and then in your onMessageReceived()
you process the notification, know the type and create a PendingIntent using the desired activity. When users clicks on the notification, android will launch the activity specified in the PendingIntent. Refer to this android documentation. I hope this helps.
[更新]我更喜欢 Bob的答案,但我从未尝试过
[Update] I like Bob's answer better but I've never tried it myself