Quickly access Jiguang Push (third-party library) in Android projects

I. Introduction

        Since I may come into contact with relevant knowledge at work, I learned how to use Aurora Push in the past two days. I also encountered a lot of problems during the learning process, so I intend to briefly summarize them.

        So, why should I access Jiguang Push? Here is a brief answer to this question. The main reason is that the timeliness of message push implemented by Aurora Push is relatively high, and it saves time and power for users' mobile phones; but if you develop a push service by yourself, simple development may lead to time-consuming and power-consuming problems, because the message The implementation of push is still relatively complicated. If an individual or a small group needs to use it, I don't think it is necessary to build a wheel; I think a large team can do it if necessary.

2. Quickly access (the most streamlined) Jiguang push notes in the Android project

        Most of the notes here come from official documents. If there are discrepancies in the content, please refer to the official documents.

        Necessary environment: Android Studio, a registered JiguangPush account.

        step 1

        Create a project in Android Studio, and then record the package name of the project, assuming the package name is com.szu.weather.

        step 2

        Create an application on the JiguangPush official website, the process will need to use the package name. If you don't understand, you can refer to the detailed steps on the official website . After the application is successfully created, you will get an AppKey, assuming the AppKey is g4u8ej62m7047000040aw5hd.

        step 3

        Configure the gradle of the Module: There are three places that need to be filled here.

        ①Add in defaultConfig braces

ndk {
    //选择要添加的对应 cpu 类型的 .so 库。
    abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
    // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}

        As shown in the picture (remember to write the package name in the picture, not xxx.xxx):

        ②Add after ndk (here, you need to fill in your AppKey, not mine)

manifestPlaceholders = [
    JPUSH_PKGNAME : applicationId,
    JPUSH_APPKEY : "g4u8ej62m7047000040aw5hd",
    JPUSH_CHANNEL : "developer-default",
]

        After adding, as shown in the figure:

        ③ Add "cn.jiguang.sdk:jpush:5.0.3" in the braces of dependencies. Since the official document says that the JCore package can be automatically pulled from version 5.0.0, we don't need to configure JCore additionally. After adding, as shown in the figure:

        After completing ③, it is no longer necessary to import the Jiguang Push third-party library through the jar package.

        Step 4

        Configure a custom Service and a custom Receiver according to the recommendation of the official document. But don't worry, these two custom classes need to be declared as public, otherwise there will be a flashback problem. I was careless and didn't add public. I found out about running the Android project Unable to instantiate application, IllegalAccessException later to solve this problem.

        ①Service needs to inherit from cn.jpush.android.service.JCommonService. If you don’t understand it at the beginning, you can leave this file empty. for example,

        Then you need to register in the AndroidManifest.xml file:

        ②Receiver needs to inherit from cn.jpush.android.service.JPushMessageReceiver. This class is similar to the Service above, and you can leave it empty if you don’t understand it at first. for example,

import cn.jpush.android.service.JPushMessageReceiver;

public class DemoReceiver extends JPushMessageReceiver {
}

        But in fact, this class is for receiving specific push messages, and you can use Bundle in this class to analyze and re-use the promotion.

        Correspondingly, the AndroidManifest.xml file also needs to register the Receiver:

        Step 5

        Create an application class, inherit from android.app.Application, and then initialize the push service in the onCreate method, for example

import android.app.Application;
import android.content.Context;

import cn.jiguang.api.utils.JCollectionAuth;
import cn.jpush.android.api.JPushInterface;

public class XXXApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);

        Context context = this;
        JPushInterface.init(context);

        JCollectionAuth.setAuth(context, true); //如初始化被拦截过,将重试初始化过程
    }
}

        This class also needs to be registered in the AndroidManifest.xml file

        Step 6

        Declare basic permissions in the AndroidManifest.xml file, the official said that it is necessary

    <!-- 记得修改相应的包名(共有两处需要修改) -->
    <permission android:name="com.szu.weather.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> 
    <uses-permission android:name="com.szu.weather.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

        step 7

        Remember to request permission in MainActivity,

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermissions();
    }

    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
        }
    }
}

        A total of 4 classes are involved (the number of lines of code is estimated to be within 50):

        Compile and run the written APP, grant permissions, view the interface, and view the notification bar, as shown in the figure:

         Step 8

        Push in the Jiguang Push platform, click "Send Preview", and then "Confirm Send":

        Step 9

        Check the push on the mobile APP (click on the push and it will disappear, and there is no other action, because I have not set it in the Receiver class), as shown in the figure:

3. References

        1. Android Quick Access - Jiguang Documentation

        2. Simple use of Jiguang Push

Four. Conclusion

        One last word. For third-party libraries, I suggest that you directly check the official documentation for configuration and use. Reading other people's blogs may cause you to encounter many pitfalls, including my blog. Technical blogs are usually time-sensitive. Don’t be angry if you step on a pit. Compared with third-party libraries, it has been iteratively updated. It is normal for blogs in the past few years to be inapplicable. Just read more official documents, remember!

Guess you like

Origin blog.csdn.net/qq_36158230/article/details/131834127