我正在为我的 android 项目使用 xamarin.forms。在将目标 api 级别从 29 更新到 33 之前,通知对我来说工作正常。更新后,没有收到 firebase 通知。这是我的代码: AndroidManifest.xml:
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<service android:name="crc6494e14b9856016c30.PNFirebaseMessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.cs:
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Mtp;
using Android.OS;
using Android.Support.V4.App;
using Firebase.Iid;
using Firebase.Messaging;
using MyPrj.Helper;
using MyPrj.Repository;
using System;
using System.Text.RegularExpressions;
namespace MyPrj.Droid.Services
{
[Service(Name = "com.fdtrucker.app.MyFirebaseMessagingService", Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
AppCenterDataProvider appCenterLog = AppCenterDataProvider.GetInstance();
Logger Logs = Logger.GetInstance();
const string TAG = "MyFirebaseMsgService";
public const string PRIMARY_CHANNEL = "default";
public override void OnMessageReceived(RemoteMessage message)
{
try
{
appCenterLog.TrackError("OnMessageReceived:", "", null);
Android.Util.Log.Debug(TAG, "From: " + message.From);
Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
if (message.Data.Keys.Count > 0)
{
var name = message.Data["key2"];
if (string.IsNullOrEmpty(name))
{
SendNotifications(message);
}
}
else
{
SendNotifications(message);
}
}
catch (Exception ex)
{
Logger.GetInstance().LogCreate("Droid/Services/MyFirebaseMessagingService/OnMessageReceived/Exceptionn: " + ex.Message);
AppCenterDataProvider.GetInstance().TrackError("Droid/Services/MyFirebaseMessagingService/OnMessageReceived/Exception", "", ex);
}
}
public void SendNotifications(RemoteMessage message)
{
try
{
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
var push = new Intent();
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0,
push, PendingIntentFlags.UpdateCurrent| PendingIntentFlags.Mutable);
NotificationCompat.Builder notification;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var chan1 = new NotificationChannel(PRIMARY_CHANNEL,
new Java.Lang.String("Primary"), NotificationImportance.High);
chan1.LightColor = Color.Green;
manager.CreateNotificationChannel(chan1);
notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
notification = new NotificationCompat.Builder(this);
#pragma warning restore CS0618 // Type or member is obsolete
}
notification.SetContentIntent(fullScreenPendingIntent)
.SetContentTitle(message.GetNotification().Title)
.SetContentText(message.GetNotification().Body)
.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
.SetSmallIcon(Resource.Drawable.icon_transparent)
.SetStyle((new NotificationCompat.BigTextStyle()))
.SetPriority(NotificationCompat.PriorityHigh)
.SetColor(0x0a384a)
.SetAutoCancel(true);
manager.Notify(id, notification.Build());
}
catch (Exception ex)
{
Logger.GetInstance().LogCreate("Droid/Services/MyFirebaseMessagingService/SendNotifications/Exceptionn: " + ex.Message);
AppCenterDataProvider.GetInstance().TrackError("Droid/Services/MyFirebaseMessagingService/SendNotifications/Exception", "", ex);
}
}
}
}
我在设备日志中收到如下错误:
Time Device Name Type PID Tag Message 04-17 11:36:55.532 小米 21091116I 警告 26745 Firebase 安装错误 与 Firebase 安装服务器 API 通信。 HTTP 响应:[403 禁止:{ “错误”:{ “代码”:403,“消息”: “请求此 API firebaseinstallations.googleapis.com 方法 google.firebase.installations.v1.FirebaseInstallationsService.CreateInstallation 被阻止了。”, “status”: “PERMISSION_DENIED”, “details”: [ { “@type”:“type.googleapis.com/google.rpc.ErrorInfo”,“原因”: “API_KEY_SERVICE_BLOCKED”,“域”:“googleapis.com”,“元数据”:{ “消费者”:“项目/ 821562894021”,“服务”: “firebaseinstallations.googleapis.com” } } ] }
任何人都请帮我解决这个问题。