Android 集成小米推送功能教程

1、前言

现在好多手机都有APP推送功能,也有好多第三方的SDK,那为什么还要集成只有小米手机好使的“小米推送”呢?因为在小米手机中,小米推送是“系统级通道”,就是在APP关闭的情况下,也能很快的到达。而第三方SDK只能通过自己的代码进行保活,效果远远达不到小米推送的效果,所以如果是小米手机,用小米推送效果会更好。

完美推送功能总结+集成极光推送:https://blog.csdn.net/f552126367/article/details/79961260

华为推送功能:https://blog.csdn.net/f552126367/article/details/84180870

2、小米推送的集成

集成小米推送还是比较简单的。

(1)需要去小米开放平台注册一个账户,然后开通开发者选项:https://dev.mi.com/console/;注册完开发者会有一个审核期,个人感觉还是比较快的。

(2)然后去控制台中注册自己的应用:如下图所示:

Android 集成小米推送功能教程_第1张图片

(3)在应用信息中找到自己程序的信息,在开发时会用到

Android 集成小米推送功能教程_第2张图片

4)集成到自己的APP程序,首先需要下载jar包,可以去官网下载源码,将Jar包下载下来,名字叫:MiPush_SDK_Client_3_6_9

(5)可参考官网给的文章:https://dev.mi.com/console/doc/detail?pId=41

(6)在AndroidMaifest配置

    
    
    
     
    


 

        
         
        
         
        
        
            
            
        
    
        
            
                
            
        

        
            
            
                
            
            
                
            
            
                
            
        

(7)在自己的Application中配置初始化小米推送功能

public class MyApplication extends Application {
    public static Context app;
    public static final String APP_ID = "2882303761517893717";
    public static final String APP_KEY = "5681789363717";
    public static final String TAG = "com.fei.main";
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        //初始化加载短信验证功能
        MobSDK.init(this);
        //初始化小米push推送服务
        if(shouldInit()) {
            MiPushClient.registerPush(this, APP_ID, APP_KEY);
        }
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        app=getApplicationContext();
        RxRetrofitApp.init(this,BuildConfig.DEBUG);
    }

    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = Process.myPid();
        for (RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

(8)创建接收信息的Message类

public class DemoMessageReceiver extends PushMessageReceiver {
    private String mRegId;
    private long mResultCode = -1;
    private String mReason;
    private String mCommand;
    private String mMessage;
    private String mTopic;
    private String mAlias;
    private String mUserAccount;
    private String mStartTime;
    private String mEndTime;
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        if(!TextUtils.isEmpty(message.getTopic())) {
            mTopic=message.getTopic();
        } else if(!TextUtils.isEmpty(message.getAlias())) {
            mAlias=message.getAlias();
        } else if(!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount=message.getUserAccount();
        }
    }
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        if(!TextUtils.isEmpty(message.getTopic())) {
            mTopic=message.getTopic();
        } else if(!TextUtils.isEmpty(message.getAlias())) {
            mAlias=message.getAlias();
        } else if(!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount=message.getUserAccount();
        }
    }
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        mMessage = message.getContent();
        if(!TextUtils.isEmpty(message.getTopic())) {
            mTopic=message.getTopic();
        } else if(!TextUtils.isEmpty(message.getAlias())) {
            mAlias=message.getAlias();
        } else if(!TextUtils.isEmpty(message.getUserAccount())) {
            mUserAccount=message.getUserAccount();
        }
    }
    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mAlias = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mTopic = cmdArg1;
            }
        } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mStartTime = cmdArg1;
                mEndTime = cmdArg2;
            }
        }
    }
    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        }
    }

(9)备注

OK,大功告成,是不是很简单,在程序中可以通过设置《别名》来识别要推送的用户,程序中可以是记录的主键ID或者是唯一标识符,。注册别名的方法如下:

MiPushClient.setAlias(this@MessagePushActivity, “123456789”, null)

你可能感兴趣的:(推送+短信)