3 回答

TA貢獻1795條經驗 獲得超7個贊
也許答案遲了,但它可以幫助其他人。
唯一的錯誤是您使用Intent代替intent(傳遞的參數)
if (Intent.Extras != null)
代替
if (intent.Extras != null)
我陷入了同樣的分心。

TA貢獻1796條經驗 獲得超7個贊
請通過這個。 實施 FirebaseMessagingService - 前臺通知
您必須創建 FirebaseMessagingService,當您的應用程序處于前臺時,您可以在其中接收 RemoteMessage。

TA貢獻1836條經驗 獲得超13個贊
實際上,根據文檔,處理前臺通知的正確方法是實現一個FirebaseMessagingService
這里有更好的解釋
您需要創建一個類似這樣的服務類:
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
// private string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageFrom = message.From;
string getMessageBody = message.GetNotification().Body;
SendNotification(message.GetNotification().Body);
}
void SendNotification(string messageBody)
{
try
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
.SetContentTitle("Title")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
catch (Exception ex)
{
}
}
}
有關更多信息,您可以查看我的 SO 問題
如何處理 Firebase 通知,即 Android 中的通知消息和數據消息
- 3 回答
- 0 關注
- 169 瀏覽
添加回答
舉報