Home > Enterprise >  FCM notification sent by FirebaseAdmin in C# cannot be received
FCM notification sent by FirebaseAdmin in C# cannot be received

Time:01-18

I am trying to send FCM notification to a particular device after some data is saved to database. However, the notification cannot be received by my mobile app running in Android emulator and built with Flutter.

I tested the same registration token from FCM console and the notification can be received.

Here is my implementation in my C#

using FirebaseAdmin.Messaging;
using Google.Apis.Auth.OAuth2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MyNeighbours.Server.Infrastructure.Services
{
    public class FirebaseService : IFirebaseService
    {
        public FirebaseService()
        {
            FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "firebase-private-key.json")),
            });
        }

        public async Task<string> SendNotification(IEnumerable<string> fcmRegistrationTokens, string title, string body)
        {
            Message message = new Message()
            {
                Token = "some valid token",
                Data = new Dictionary<string, string>()
                {
                    {"title", title},
                    {"body", body},
                },
            };
            var response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
            return response;
        }
    }
}

Every time when I send a notification, response always shows succeeded with a message id. I can also confirm the private key json file is loaded.

Can anyone please help? Thank you

CodePudding user response:

try to add Notification in Message{} and set sound and priority:

Notification = new Notification()
                    {
                        Title = Title,
                        Body = Body
                    },
                    Android = new AndroidConfig()
                    {
                        Notification = new AndroidNotification()
                        {
                            Sound = "default",
                            Priority = NotificationPriority.MAX
                        }
                    },
                    Apns = new ApnsConfig()
                    {
                        Aps = new Aps()
                        {
                            Sound = "default"
                        }
                    }
  • Related