0

I am using xamarin.forms for my android project. Before updating targeting api level from 29 to 33, notification is working fine for me. After updating, firebase notification is not receiving. Here is my code: 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);
      }
    }
  }
}

I got Error like below on Device log:

Time Device Name Type PID Tag Message 04-17 11:36:55.532 Xiaomi 21091116I Warning 26745 Firebase-Installations Error when communicating with the Firebase Installations server API. HTTP response: [403 Forbidden: { "error": { "code": 403, "message": "Requests to this API firebaseinstallations.googleapis.com method google.firebase.installations.v1.FirebaseInstallationsService.CreateInstallation are blocked.", "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "API_KEY_SERVICE_BLOCKED", "domain": "googleapis.com", "metadata": { "consumer": "projects/821562894021", "service": "firebaseinstallations.googleapis.com" } } ] }

Anyone please help me to resolve this issue.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Manthiram
  • 149
  • 2
  • 13
  • There is a case about [Firebase: 403 PERMISSION_DENIED (FirebaseError: Installations): Requests are blocked, after updating SDKs (FirebaseInstallationsService)](https://stackoverflow.com/questions/58495985/firebase-403-permission-denied-firebaseerror-installations-requests-are-blo) has the same web request message as yours. You can refer to it. – Liyun Zhang - MSFT Apr 18 '23 at 06:56

0 Answers0