Access to the premise

  1. The target API level is 16 (Jelly Bean) or higher

  2. Use Gradle 4.1 or higher

  3. Using Jetpack (AndroidX), you need to meet the following version requirements:

    • com.android.tools.build:gradle3.2.1 or higher
    • compileSdkVersionVersion 28 or higher
  4. You need to install the Google Play service on your device or emulator

Firebae creates applications

1. Click Add Project

2. Give the project a nice name

3. Enable Analytics

4. Select the Analytics account and create the application

Applications access

1. Click the Android icon

2. Enter the application package name and register the application

3. Download the JSON file in the red box and place it in the main Module (app in the screenshot) directory

4. Add the Firebase SDK

  1. Project level build. Gradle
buildscript { repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository } dependencies { ... // Add this line classpath 'com.google. GMS: Google-services :4.3.8'}} allprojects {... repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository ... }}Copy the code
  1. Application level build. Gradle
apply plugin: 'com.android.application' // Add this line apply plugin: 'com. Google. GMS. Google - services' dependencies {implementation platform (' com. Google. Firebase: firebase - bom: 28.3.0')}Copy the code

Firebase Cloud Messaging

1. Add dependencies

  • A:
Implementation of platform (' com. Google. Firebase: firebase - bom: 28.2.0 ') implementation 'com.google.firebase:firebase-messaging'Copy the code
  • Method 2:
Implementation 'com. Google. Firebase: firebase - messaging: 22.0.0'Copy the code

2. Push notifications

Push to all users

Click on the Cloud MessagingChoose Send Your First MessagingFill in the title and content you want to send, then click NextSelect the app you want to push, and click Approve directly (default to send immediately)

Push test message

Access Token

FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
            if(! task.isSuccessful()) { Log.w(TAG,"Fetching FCM registration token failed", task.getException());
                return;
            }
            String token = task.getResult();
            Log.e(TAG, token);
            Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show(); }});Copy the code

Click On New notificationFill in the title and content you want to send, then click Send Test MessageFill in the Token obtained in the first step and click test

Push to users who subscribe to a topic

AS Mock Subscription — Take the world Boss refresh AS an example

FirebaseMessaging.getInstance().subscribeToTopic("Boss_Refresh")
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if(! task.isSuccessful()) { Log.w(TAG,"Subscription failed", task.getException());
                }
                Toast.makeText(MainActivity.this."Subscription successful", Toast.LENGTH_SHORT).show(); }});Copy the code

Fill in the title and content you want to send, then click NextSelect the theme, fill in the theme Boss_Refresh from the first simulation, and then click Audit

Google Analytics(Event reporting and Analysis)

1. Add dependencies

  • A:
Implementation of platform (' com. Google. Firebase: firebase - bom: 28.2.0 ') implementation 'com.google.firebase:firebase-analytics'Copy the code
  • Method 2:
Implementation 'com. Google. Firebase: firebase - analytics: 19.0.0'Copy the code

Ps: Remember to add network permissions

<uses-permission android:name="android.permission.INTERNET" />

2. Set a burying point

Throw 2 random buttons on the layout and bind click events

Key code:

FirebaseAnalytics. GetInstance (this). The logEvent (” button 1 click event “, bundle). Code demo:

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_1:
                bundle.putString("event_1"."1111111111");
                FirebaseAnalytics.getInstance(this).logEvent("Button 1 click event",bundle);
                Log.e(TAG, "Execute incident report 1");
                break;
            case R.id.button_2:
                bundle.putString("event_2"."222222222");
                FirebaseAnalytics.getInstance(this).logEvent("Button 2 click event",bundle);
                Log.e(TAG, "Execute event report 2");
                break;
            default:
                break; }}Copy the code

3. Test in DebugView mode

Why is the third step of the DebugView test, mainly because the normal event reporting is a certain delay, can not give real-time feedback to the developer, and the DebugView is a real-time display of reported events on the console interface.

First, start debugging mode,

The adb shell setprop debug. Firebase. Analytics. The app package name

Example:

adb shell setprop debug.firebase.analytics.app com.example.test

The console starts the DebugView

If the operation is normal, the test phone model is displayed in the upper left corner of the DebugView interface

If the phone model is not displayed, try the following operations:

  • Verify that the above ADB command to start debug mode is correct
  • Check whether USB debugging is enabled on the phone
  • Check whether the VPN is enabled
  • Data line unplug try again

The following is the data presentation

4. Events

DebugView mode has data that basically there is no problem, here is mainly to show the normal event report appearance

If you like the article, pay attention to my public number Android tomato jun bar!