Reference: firebase.google.com/docs/cloud-…
1, the introduction of
Speaking of Firebase Cloud Messaging (referred to as FCM), most people may not know it, but speaking of GCM (Google Cloud Messaging), surely Android developers are aware of it. Although the current Domestic Android devices have neutered Google services, resulting in GCM can not be used normally. But GCM still has a reputation.
2. Working principle
Even shut down the apple mobile phone software, mobile phone can still receive software push messages, the reason is that the message is from software server via the apple server forwarding to apple mobile phone, and the role of GCM with the apple push principle, software server message to Google server, Google servers will be pushed to the android device, It can greatly reduce the wake up of useless software and reduce the power consumption of the phone.
3, the status quo
Because Google blocked access at home, most of the domestic mobile phone are castrated Google services, resulting in the GCM is not available, each big manufacturers to maintain their own software is active on android devices, to facilitate the various messages pushed to the android devices, leading people to impression of android is caton and power consumption, This has a lot to do with the unavailability of GCM.
Due to the business needs of the company, we need to access GCM, search the documents on Google, and find that the official no longer recommends developers to use GCM, but recommends a more powerful FCM, which can be considered as an improved or enhanced version of GCM.
Ii. Preparation for access
1. Develop equipment
An Android phone with Google servers
2. Development environment
Android Studio
3. Console Settings
(1) Create an application
Open the Firebase console:console.firebase.google.com/
Click on theNew project
(2) Fill in the project name
In the window that pops up, fill in the project name and clickCreate a project
(3) Integrate Firebase into Android
inOverview
Click on the optionsAdd Firebase to your Android application
(4) Fill in the package name and SHA1
Enter the project’s value in the pop-up windowThe package name
andsha1
And then clickAdd application
(5) Download Google-services. json
Click after the creation is successfulDownload google-services.json
, then switch the Project to the Project view and place the downloaded files in the root directory of your app
PS: Remember, if the sha1 added is a Debug version, then the official sha1 needs to be added as well. Click the Settings button on the Overview TAB, select project Settings, go to the general TAB, add sha1 below, and then re-download Google-services. json and place it in the root directory of your app
Three, Android terminal integration FCM
Reference: firebase.google.com/docs/cloud-…
1. Modify gradle files
(1) Project-level build.gradle
Add the following code to the Dependencies node in the build.gradle file at the project level
The classpath 'com. Google. GMS: Google - services: 3.0.0'Copy the code
(2) Build. Gradle
Add dependencies to the build.gradle file at the application level
The compile 'com. Google. Firebase: firebase - messaging: 10.0.2'Copy the code
Put the following code on the last line of the file
apply plugin: 'com.google.gms.google-services'Copy the code
2, create MyFirebaseInstanceIDService
In the project of new MyFirebaseInstanceIDService, inherit FirebaseInstanceIdService, and write down the following method
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}Copy the code
Register the Service in the manifest file
< service android:name=".MyFirebaseInstanceIDService">
< intent-filter>
< action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
< /intent-filter>
< /service>Copy the code
3, MyFirebaseMessagingService
New MyFirebaseMessagingService, inherit FirebaseMessagingService, rewrite the following method
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}Copy the code
Register in the manifest file
< service
android:name=".MyFirebaseMessagingService">
< intent-filter>
< action android:name="com.google.firebase.MESSAGING_EVENT"/>
< /intent-filter>
< /service>Copy the code
4. Obtain the token
In a project, the following code can be used to obtain the token of the device and send the token to the server so that the server can push the message of the specified device
FirebaseInstanceId.getInstance().getToken();Copy the code
5. Set the icon and background color
Add the following code to the manifest file to set the notification icon and the icon’s background color, respectively
< meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/icon_logo" />
< meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimary" />
Copy the code
Send a message
1. The console sends messages
Select Notifications on the left side of the console, then click Write New Message to send a message to the specified device or all devices
2. Send a message by sending a POST request
(1) Request address
The requested address is: fcm.googleapis.com/fcm/send
(2) Request header
The HTTP header must contain two parts
Content-Type:application/json
Authorization:key=YOUR_SERVER_KEY
Serverkey can be obtained from console Settings – project Settings – cloud messaging – serverkey
(3) Send content
If you send a notification message, you send something similar to the code below
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}Copy the code
The value of to is the token of the device
More parameter Settings can reference: firebase.google.com/docs/cloud-…