📜 ⬆️ ⬇️

Push notifications to Android using Firebase Cloud Messaging for beginners

Not so long ago, Google bought Firebase and now on the page with a description of GCM we are met with the good news that it’s time to migrate to Firebase.
... GCM users are strongly advised to upgrade to FCM.

In this article, we will analyze the test project and consider one pitfall associated with the notification icon.

We will need:


Step 1.
')
Download the demo project and open it in your favorite Android Studio.

Step 2.

Now we need to go to the Firebase console and add our project.



Then click "Add Firebase to your Android app."



The name of the package, if you are doing everything in manul: "com.google.firebase.quickstart.fcm".

In the second step, download the google-service.json file and add it to the project.



We press it is ready, dependences are already added in the test project.

Step 3.

The preparatory part is finished, you can press the "Run application" button

During the compilation process you can get 2 errors:

Missing api_key/current key with Google Services 3.0.0

In this case, you need to go to the project settings and restart the google-service.json file. This is a strange bug , but the benefit is easily treated.

The second error is:

Failed to resolve: com.google.firebase:firebase-core:9.0.0

Then you need to upgrade your Google Play Service and Google Repository. Link to Stackoverflow .

After launching the application, in Android monitor you will see our token. You can go to test. We go to the console, look for our application, and go to the Grow-> Notifications section:



Click to create a message:



Enter our data, click send and watch in Android monitor how it all came.

Now to the most interesting

The most common question is: how can I change the notification icon when the application is running in the background?

The fact is that when an application is in the background, it is sent immediately to the Notification center, and the function

 onMessageReceived 

does not work.

To use your notification icons, you need to send a message through the API, through the console this customization does not work at the moment.

UPD: If you want all your messages, even when the application is in background or killed, are processed using
 onMessageReceived 

in the application, send data messages, i.e. no notification section

API reference . For tests, you can use the good old CURL.

Another useful link

That's basically all you need to know to try new push notifications.

Source: https://habr.com/ru/post/302002/


All Articles