Search code examples
c#audiofirebase-cloud-messagingicons

Howto define icon for C# fcm push notification


I send a notification like this

FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile("private_key.json")
            });

            // This registration token comes from the client FCM SDKs.
            var registrationToken = "TOKEN_HERE";

            // See documentation on defining a message payload.

            var message = new Message()
            {
                Data = new Dictionary<string, string>()
    {
        { "myData", "1337" },
    },
              
           Token = registrationToken,
                //Topic = "all",
                Notification = new Notification()
                {
                    Title = "Test from code",
                    Body = "Here is your test!"
                }
            };

            // Send a message to the device corresponding to the provided
            // registration token.
            string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
            // Response is a message ID string.
            Console.WriteLine("Successfully sent message: " + response);

I want to define the icon associated to the notification and the sound for ios and android ? Is it in this code that i must specify these information or in the application and how ?

Thanks


Solution

  • The icon is a property called ImageUrl when you initialize the Notification object:

               Notification = new Notification()
               {
                   Title = "Test from code",
                   Body = "Here is your test!",
                   //Add this
                   ImageUrl = "somePath.jpg"
               }
           };
    

    And for sound, you need to initialize another object called Apns, like this:

    Apns = new ApnsConfig()
    {
        Aps = new Aps()
        {
            //Change this for the sound you would like
            Sound = "default"
        }
    }
    

    Entire solution based in the code you provided:

    FirebaseApp.Create(new AppOptions()
                {
                    Credential = GoogleCredential.FromFile("private_key.json")
                });
    
                // This registration token comes from the client FCM SDKs.
                var registrationToken = "TOKEN_HERE";
    
                // See documentation on defining a message payload.
    
                var message = new Message()
                {
                    Data = new Dictionary<string, string>()
        {
            { "myData", "1337" },
        },
                  
               Token = registrationToken,
                    //Topic = "all",
                    Notification = new Notification()
                    {
                        Title = "Test from code",
                        Body = "Here is your test!"
                        //Add this
                        ImageUrl = "somePath.jpg"
                    },
                    Apns = new ApnsConfig()
                    {
                        Aps = new Aps()
                        {
                            //Change this for the sound you would like
                            Sound = "default"
                        }
                    }
                };
    
                // Send a message to the device corresponding to the provided
                // registration token.
                string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
                // Response is a message ID string.
                Console.WriteLine("Successfully sent message: " + response);