For the push service of overseas apps, try cri!!! -Alibaba Cloud Developer Community

I. Preface

there are many domestic service providers of push service, but if it is an overseas App, it is recommended to use Google's own push service. After all, the commonly used mobile phone models overseas all have Google service.

Google's push service, formerly known as GCM(Google Cloud Message). Since Google acquired Firebase, it has pushed the service to a service of Firebase, which is now called CRI.

This article will give a detailed explanation of the process of integrating CRI under the Android.

II. What is Firebase

Firebase was originally a real-time back-end database startup company, mainly used to help developers quickly write Web and mobile applications, similar to the domestic Bmob back-end cloud services, allowing front-end or client developers, without writing a line of server code, you can quickly develop a reference by simply configuring it.

Bmob I just read the document and have not actually used it. I will not comment on the details.

Later, Firebase was acquired by Google and began to use Google's cloud services. Some Google's own services were added to enrich its functions.

In addition to Firebase, Android also supports other languages and platforms, such as iOS, C ++, and Unity.

Basically, all the functions that you can think of are supported by Firebase. I think these functions are easy to use: authorized login, push, real-time database, CDN storage, crash report, event statistics, dynamic invitation, advertising, and promotion with back-to-origin.

Therefore, if you need to develop an App for overseas use, Firebase is a good choice. Firebase use modular integration, you can integrate whatever functions you use.

For more information about Firebase, see the official documentation:

https://firebase.google.com/docs/

if you need to use Firebase functions, you must integrate Firebase basic services into the project.

The integration of Firebase is very simple. You only need to register a Google account, create an App in it, configure the corresponding configuration, and download the google-service.json configuration file of the App, place it in the corresponding position of the project.

There is nothing to elaborate on. Just read the official documents and go step by step.

https://firebase.google.com/docs/android/setup

the final generated App looks like this:

you can download it in the settings google-services.json file.

3. Use cri

at this point, you basically think that you have successfully integrated the Firebase into your project, and then you begin to integrate the robust module.

3.1 settings Firebase and FCM SDK

in the build.gradle file, add the dependency of the CRI.

The official example uses 10.2.6, but the new version of 11.0.4 is actually recommended.

3.2 modify AndroidManifest.xml

the two main Service of CRI need to be configured in the Manifest.

1. Inherited from FirebaseMessagingService services.

It is mainly used to process the Push received when the App is running. Generally speaking, we do not only want to receive the Push received when the App is not running, so we usually need to integrate it.

2. Inherit FirebaseInstanceIdService services.

It is mainly used to manage the registration token (hereinafter referred to as FCM_TOKEN) and change of CRI. It can obtain the unique FCM_TOKEN of the user device, which is used when pushing messages to a single user.

3.3 two key services in CRI

1, MyFirebaseMessagingService

let's look at its structure first.

Here, what we really need to care about is onMessageReceived()method, which is called back when the push is received. Note that the methods here are all in WorkerThread it is completed in the thread, so pay extra attention to the UI operation.

In onMessageReceived()method callback, will pass a RemoteMessage it contains all the pushed content received. We need to perform additional operations based on the information sent from this push. For example, pop-up reminders and create a Notification.

2, MyFirebaseInstanceIDService

continue to look at its structure.

In fact, there are some confusing internal methods, but what we really care about is only onTokenRefresh()this method calls back when the FCM_TOKEN (called the token in the document) is refreshed.

Normally, when the App starts, it is assigned a FCM_TOKEN, which can FirebaseInstanceId.getInstance().getToken()this Api is obtained, but it is not static. When it changes, it will callback onTokenRefresh()method. Therefore, if there is a business logic for pushing a single user, it needs to be reported to your server, the current user's FCM_TOKEN.

At this point, the integration of the CRI is basically completed. For more information, we recommend that you see the official documentation.

https://firebase.google.com/docs/cloud-messaging/android/client

3.4 start pushing

after you have configured everything, you can Firebase modules in the background of the Notifications to push messages. (The Push function may also be implemented by the server)

first, you need to configure a content, that is Message text .

Of course, the same as many pushes, this feature supports pushing all users, Topic groups, and individual devices. Only the specified devices are pushed here.

You can also configure a Title , and it supports the latest Android O Notification Channel configuration. If necessary, you can configure the passed parameters.

Click SEND MESSAGE button, you can receive your push message on the device.

Effect after expansion:

at this point, it can basically be regarded as the integration and use of CRI.

IV. Pits in the use of CRI

the previous content is basically what you can find in the official documents. The next part is about dry goods. It is said to be working, but it is some problems encountered in the process of using.

4.1 whether the App runs or not determines the two lines pushed.

When the App is running, if there is a Notification push, it is usually controlled by ourselves, so we can customize the final effect after it is clicked through PendingIntent.

However, if the App is not running, it is entirely up to the CRI service to help you complete this series of operations. The effect after clicking it can only adjust your App, and pass the parameters you need to your SplashActivity(Action is android.intent.action.MAIN Activity).

Therefore, we need to consider two cases: data transmission has responded, which need to be discussed according to the business. Empty chat is meaningless.

4.2 push service icon and font color

as shown in the preceding figure, if the application is not running, it is completely uncontrolled. However, CRI still provides us with the place to configure the icon. If you do not configure it, you will be pushed by a small white square icon.

You can configure the icon and the color of the App name for the push notification of the AVA. For example, in the previous Demo example, you can directly use the Demo image on Github of the Firebase.

The icon and font color must be configured in AndroidManifest.xml.

It is also important to note that usually the icons of our App are very exquisite, but this kind of Icon cannot be directly used in the push of the CRI. Additional customization and corresponding size are required.

FCM Icon standards: the background is transparent and filled with white patterns. . (In fact, the expanded effect will color the icon, so any color will eventually be colored to the color we configured. If not configured, it will be light gray by default).

And its size is shown in the following table:

of course, it is the same as the target. It does not need a full set, but only needs to be configured with the required size.

If you configure these, you will still get a small white block. You can try to upgrade the version of CRI. The earlier version seems to have this Bug, which has been solved in the new version.

V. Conclusion

here, basically some details of the use of CRI are explained clearly. If you have any questions, we recommend that you refer to the official documentation or directly see the Demo on Github.

This article is transferred from chengxiangmoying blog Park blog, original link: http://www.cnblogs.com/plokmju/p/7466332.html,如需转载请自行联系原作者

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now