Push Notification In Xamarin.Android Using Google Cloud Messaging (GCM)

Create a New Project. Select Single-View App (Android) under Installed > Visual > C# >Android.



Open AndroidManifest.xml file under your Android project's properties.



Copy the value of package attribute in manifest tag.



Open your Browser and go to https://console.firebase.google.com . Sign-in with your Google account to create a new one.

Click Add Project. Choose a project name. Here, the name is PushNotificationGCM.



Click Add Firebase to your Android app.



Paste the package name copied in Step 3 and click Register app



Click Download google-services.json and click Continue.
Copy google-services.json file into an Android project



Include google-services.json file into Project's solution by clicking View All Files in Solution Explorer tab. Right-click google-services.json file and click Include in project. 



Right-click Android project and click Manage NuGet Packages.



Click Browse tab, search for Xamarin.GooglePlayServices.Base and install the package.
Search for another NuGet Package Xamarin.Firebase.Messaging and install the package.



Right-click project to build the solution. Now, Close and re-open Visual Studio.

Right-click on google-services.json file and click Properties.



Select Build Action to be GoogleServiceJson. 



Open AndroidManifest.xml under Android project's Properties.
Right-click Android project and click Properties. Go to Android Manifest tag and check "INTERNET" checkbox under the required permissions.



Add a new C# code file. Name it according to your project. Here, it's name is DemoFirebaseIdServices.cs. Include the code given below.



Build the solution and run the project in debug mode on an Android device. Make sure that the device is connected to the internet and Google Play Services are installed on the device.



Once the project is running, watch the Output Window in Visual Studio, where refresh token will appear. Copy the token.

Open your Firebase project in the Browser, which you created in Step 5. Click Notification menu in Navigation bar on the right and click Send your first message.



On the Compose message page, add message text, add message label, select Single device under Target section and add registration token copied in Step 22 and click Send message. 



The notification will then be sent to Single device and the next page shows the status of sent notification message.



Completed status of the notification message shows that the notification has successfully reached the target device. 



The project is available on GitHub at th URL https://github.com/imranaftab/xamarin-android-push-notification-with-google-cloud-messaging-gcm

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now