2.进入后创建项目,按照步骤创建完后如下
3.后台配置完了,我们再配置代码,第一次使用小米推送 我下了个Demo再把里面有用的复制到自己项目中:
先把jar包复制到自己项目中
首先在继承了Application的类中放入
private static final String APP_ID = "2882303761517483058";// user your appid the key.private static final String APP_KEY = "5951748376058"; // 此TAG在adb logcat中检索自己所需要的信息, 只需在命令行终端输入 adb logcat | grep// com.xiaomi.mipushdemopublic static final String TAG = "com.dodonew.epapp";Id 和key什么的都是在小米开放平台创建项目获得的
if (shouldInit()) {MiPushClient.registerPush(this, APP_ID, APP_KEY); } LoggerInterface newLogger = new LoggerInterface() { @Overridepublic void setTag(String tag) { // ignore} @Overridepublic void log(String content, Throwable t) { Log.d(TAG, content, t);} @Overridepublic void log(String content) { Log.d(TAG, content);} }; Logger.setLogger(this, newLogger); if (sHandler == null) {sHandler = new DemoHandler(getApplicationContext()); }其中shouldInit()和Handler:
private boolean shouldInit() { ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)); List<RunningAppProcessInfo> 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;} public static DemoHandler getHandler() { return sHandler;}public static class DemoHandler extends Handler {private Context context;public DemoHandler(Context context) {this.context = context; }@Override public void handleMessage(Message msg) {String s = (String) msg.obj;if (sMainActivity != null) { sMainActivity.refreshLogInfo();}if (!TextUtils.isEmpty(s)) { // Toast.makeText(context, s, Toast.LENGTH_LONG).show();} }}说实话Demohander其实没什么用,主要是弹出toast提示而已,我不喜欢 于是隐藏了toast
public void refreshLogInfo() { String AllLog = ""; for (String log : logList) {AllLog = AllLog + log + " "; } System.out.println("mainActivity中消息推送::::::::"+AllLog);}然后 我们要把Demo中的一个广播类复制过来 ,由于内容一样我就不复制了
Intent intent = new Intent(context, 指定的Activity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);最后 我们要去配置AndroidManifest.xml
<permission android:name="com.dodonew.epapp.permission.MIPUSH_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.dodonew.epapp.permission.MIPUSH_RECEIVE" /><uses-permission android:name="android.permission.VIBRATE" />在<Appliction/>中添加
<serviceandroid:name="com.xiaomi.push.service.XMJobService"android:enabled="true"android:exported="false"android:permission="android.permission.BIND_JOB_SERVICE"android:process=":pushservice" /><serviceandroid:name="com.xiaomi.push.service.XMPushService"android:enabled="true"android:process=":pushservice" /><serviceandroid:name="com.xiaomi.mipush.sdk.PushMessageHandler"android:enabled="true"android:exported="true" /> <serviceandroid:name="com.xiaomi.mipush.sdk.MessageHandleService"android:enabled="true" /><receiverandroid:name="com.dodonew.epapp.control.receiver.XiaoMiMessageReceiver"android:exported="true"><intent-filter> <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /></intent-filter><intent-filter> <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /></intent-filter><intent-filter> <action android:name="com.xiaomi.mipush.ERROR" /></intent-filter> </receiver> <receiverandroid:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"android:exported="true"><intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><category android:name="android.intent.category.DEFAULT" /></intent-filter> </receiver> <receiverandroid:name="com.xiaomi.push.service.receivers.PingReceiver"android:exported="false"android:process=":pushservice"><intent-filter> <action android:name="com.xiaomi.push.PING_TIMER" /></intent-filter> </receiver>就可以了。