Firebase push notification with json payload. Implementation on Xamarin Android to display notification with title, body and Image.
- Install following nuget package inside Xamarin Android Project
- Xamarin.Firebase.Messaging
- Xamarin.AndroidX.RecyclerView
- Xamarin.Google.Dagger
- Get server key from firebase console and download google-services.json file
- Place goole-services.json inside android project and set build action to GoogleServices.Json
public class NotificationPayload
{
[JsonProperty("to")]
public string To { get; set; }
[JsonProperty("data")]
public Data Datas { get; set; }
}
public class Data
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("body")]
public string Body { get; set; }
[JsonProperty("ImageUrl")]
public string ImageUrl { get; set; }
}
You can import postman_collection.json to postman and push Firebase Notification. Make sure you replace header key with your server key.
{
"to":"/topics/all",
"data":
{
"title":"Discount 📣📣",
"body":"Get 20% off with discount on peppers code 'NEW YEAR'",
"ImageUrl":"https://homepages.cae.wisc.edu/~ece533/images/peppers.png"
}
}
Make sure you create your notification channel and suscribe to the topic that you are using in your payload 'to' parameter. If you want to notify specific user, replace topic with user's firebase id.
Subscribe Notification FirebaseMessaging.Instance.SubscribeToTopic("all");
For more details refer the repo code above.
