Android integrated Aurora push

First step

Download the aurora push SDK, Download address: https://docs.jiang.cn/jpush/resources/

After decompression, enter libs to copy the jar to the project libs folder. main, create a new jniLibs folder. Pay attention to the level with java, copy the so library you want to rely on, or put it into libs at the same time (note that right click Add As Library for jar)

The second step

Android manifest manifest file configuration

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.helloworld.jpushdemo">

    <permission
        android:name="com.helloworld.jpushdemo.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />

    <!-- Requirement -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.helloworld.jpushdemo.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <!-- For opening debug Version 6.0 System Cascade window permission -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.REAL_GET_TASKS" />

    <application
        android:name=".MyAppaction"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Required SDK Core functions-->
        <!-- Configurable android:process Parameter will PushService Put in another process -->
        <service
            android:name="cn.jpush.android.service.PushService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTER" />
                <action android:name="cn.jpush.android.intent.REPORT" />
                <action android:name="cn.jpush.android.intent.PushService" />
                <action android:name="cn.jpush.android.intent.PUSH_TIME" />
            </intent-filter>
        </service>

        <!-- since 3.0.9 Required SDK Core functions-->
        <provider
            android:authorities="com.helloworld.jpushdemo.DataProvider"
            android:name="cn.jpush.android.service.DataProvider"
            android:exported="true" />

        <!-- since 1.8.0 option Optional. For different applications in the same device JPush The function that services pull each other up. -->
        <!-- If this function is not enabled, this component can be deleted, and other applications will not be pulled or pulled by other applications -->
        <service
            android:name="cn.jpush.android.service.DaemonService"
            android:enabled="true"
            android:exported="true">
            <intent-filter >
                <action android:name="cn.jpush.android.intent.DaemonService" />
                <category android:name="com.helloworld.jpushdemo"/>
            </intent-filter>
        </service>

        <!-- Required SDK Core functions-->
        <receiver
            android:name="cn.jpush.android.service.PushReceiver"
            android:enabled="true" >
            <intent-filter android:priority="1000">
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
                <category android:name="com.helloworld.jpushdemo"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <!-- Optional -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <!-- Required SDK Core functions-->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.helloworld.jpushdemo" />
            </intent-filter>
        </activity>

        <!-- Required SDK Core functions-->
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />

        <!-- Required since 3.0.7 -->
        <!-- New tag/alias The interface result return requires the developer to configure a customized broadcast -->
        <!-- The broadcast needs to inherit JPush Provided JPushMessageReceiver class, And add a new one as follows Intent-Filter -->
        <receiver
            android:name=".MyJPushMessageReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
                <category android:name="com.helloworld.jpushdemo" />
            </intent-filter>
        </receiver>

        <!-- User defined. User defined broadcast receiver-->
        <receiver
            android:name=".MyReceiver"
            android:enabled="true">
            <intent-filter>
                <!--Required User registration SDK Of intent-->
                <action android:name="cn.jpush.android.intent.REGISTRATION" />
                <!--Required User reception SDK News intent-->
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                <!--Required User reception SDK Of notification bar information intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                <!--Required User opens the intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                <!-- Receive network change connection/To break off since 1.6.3 -->
                <action android:name="cn.jpush.android.intent.CONNECTION" />
                <category android:name="com.helloworld.jpushdemo" />
            </intent-filter>
        </receiver>

        <!-- Set up AppKey -->
        <meta-data android:name="JPUSH_APPKEY" android:value="b6fe630e2bce584839547ce0"/>

    </application>
</manifest>
Tips:
Pay attention to the replacement of package names, respectively in 6, 54, 66, 76, 99, 114, 133 and 138 (AppKey)

The third step

MyAppaction initialization

package com.helloworld.jpushdemo;

import android.app.Application;
import cn.jpush.android.api.JPushInterface;

public class MyAppaction extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //TODO: set to turn on the log. Please turn off the log when publishing
        JPushInterface.setDebugMode(false);
        //Initialize Aurora push
        JPushInterface.init(this);
    }
}

The fourth step

MyJPushMessageReceiver

package com.helloworld.jpushdemo;

import android.content.Context;
import android.util.Log;

import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * Status callback
 */

public class MyJPushMessageReceiver extends JPushMessageReceiver {

    private static final String TAG = "MyJPushMessageReceiver";

    /**
     * tag Adding, deleting, querying and modifying will call back the result in this method
     */
    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
        //The following two callbacks are similar
        Log.e(TAG, "onTagOperatorResult Alias from query: " + jPushMessage.getAlias());
        Log.e(TAG, "onTagOperatorResult Tags from query: " + jPushMessage.getTags());
        Log.e(TAG, "onTagOperatorResult Error code 0 is successful: " + jPushMessage.getErrorCode());
        Log.e(TAG, "onTagOperatorResult Incoming token: " + jPushMessage.getSequence());
    }

    /**
     * Querying the binding state of a tag to the current user will call back the result in this method
     */
    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
        Log.e(TAG, "onCheckTagOperatorResult Error code 0 is successful: " + jPushMessage.getErrorCode());
    }

    /**
     * alias Related operations call back results in this method
     */
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
        Log.e(TAG, "onAliasOperatorResult Error code 0 is successful: " + jPushMessage.getErrorCode());
    }
}

The fifth step

MyReceiver

package com.helloworld.jpushdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

/**
 * Custom broadcast receiver
 */
public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = "MyReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {

            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e(TAG, "JPush User registration successful id: " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {

            Log.e(TAG, "Receive the pushed custom message id: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.e(TAG, "Receive push notification id: " + notifactionId);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {

//                Intent i = new Intent(context, TestActivity.class);
//                i.putExtras(bundle);
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(i);
            Log.e(TAG, "The user clicks to open the notification jump Activity: ");
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {

            Log.e(TAG, "onReceive: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);

            Log.e(TAG, "onReceive: " + intent.getAction() + " Connection state change " + connected);
        } else {
            Log.e(TAG, "onReceive:  Unprocessed intent- " + intent.getAction());
        }
    }
}

The sixth step

NotificationsUtils

package com.helloworld.jpushdemo;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Judge whether the notice column is open
 */

public class NotificationsUtils {

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    @SuppressLint("NewApi")
    public static boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null;
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }
}

The seventh step

MainActivity

package com.helloworld.jpushdemo;

import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;

/**
 * Document: https://docs.jiguang.cn/jpush/client/Android/android API/
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

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

        if (NotificationsUtils.isNotificationEnabled(this)) {
            Log.e(TAG, "onCreate: Notification permission turned on");
            setBasicSetup(1);
            setBasicSetup(4);
        } else {
            Log.e(TAG, "onCreate: Notification permission is not enabled");
            //Prompt the user to set and jump to the application information interface
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
        }
    }

    /**
     * 1-2-3-4
     * Add, delete, modify, check
     */
    public void setBasicSetup(int type) {
        if (type == 1) {
            //Set alias (new call will overwrite previous settings)
            JPushInterface.setAlias(this, 0, "0x123");
            //Set label (same as above)
            JPushInterface.setTags(this, 0, setUserTags());
        } else if (type == 2) {
            //Delete aliases
            JPushInterface.deleteAlias(this, 0);
            //Delete specified label
            JPushInterface.deleteTags(this, 0, setUserTags());
            //Delete all labels
            JPushInterface.cleanTags(this, 0);
        } else if (type == 3) {
            //Increase the number of tag users (usually the user ID is set as the target after login, but it is rare to add new users elsewhere)
            JPushInterface.addTags(this, 0, setUserTags());
        } else if (type == 4) {
            //Query all tags
            JPushInterface.getAllTags(this, 0);
            //Query aliases
            JPushInterface.getAlias(this, 0);
            //Query the status of the specified tag bound to the current user (obtained by MyJPushMessageReceiver)
            JPushInterface.checkTagBindState(this, 0, "0x123");
            //Get registration id
            JPushInterface.getRegistrationID(this);
        }
    }

    /**
     * Tagging user
     */
    private static Set<String> setUserTags() {
        //Add 3 labeled users (it is common to get login userid)
        Set<String> tags = new HashSet<>();
        tags.add("0x123");
        tags.add("0x124");
        tags.add("0x125");
        return tags;
    }
}

The eighth step

Go to the background of aurora to push, click send now to receive the notice

Write at the end:
Reference article address: http://blog.csdn.net/qq_34536167/article/details/78946437
Thank you for your advice~

Tags: Android SDK Java xml

Posted on Tue, 31 Mar 2020 09:10:54 -0700 by p0pb0b