Android小米推送简单使用方法

Android小米推送简单使用方法

公司项目需要做推送,我们选择用小米推送,经过一段时间的摸索,终于可以简单的使用小米推送了。

1.创建账号登入后 登入后选择消息推送:

Android小米推送简单使用方法

2.进入后创建项目,按照步骤创建完后如下

Android小米推送简单使用方法

3.后台配置完了,我们再配置代码,第一次使用小米推送 我下了个Demo再把里面有用的复制到自己项目中:

先把jar包复制到自己项目中

Android小米推送简单使用方法

首先在继承了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.mipushdemo 
 public static final String TAG = "com.dodonew.epapp"; 

Id 和key什么的都是在小米开放平台创建项目获得的
再在Appliction的oncreate()方法中加入:

if (shouldInit()) { 
   MiPushClient.registerPush(this, APP_ID, APP_KEY); 
  } 
  LoggerInterface newLogger = new LoggerInterface() { 
 
   @Override 
   public void setTag(String tag) { 
    // ignore 
   } 
 
   @Override 
   public void log(String content, Throwable t) { 
    Log.d(TAG, content, t); 
   } 
 
   @Override 
   public 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
其中MainActivity中的refreshLogInfo()方法:

public void refreshLogInfo() { 
  String AllLog = ""; 
  for (String log : logList) { 
   AllLog = AllLog + log + "\n\n"; 
  } 
  System.out.println("mainActivity中消息推送::::::::"+AllLog); 
 } 

然后 我们要把Demo中的一个广播类复制过来 ,由于内容一样我就不复制了
其中有个方法很重要: onNotificationMessageClicked(Context context, MiPushMessage message)

这个方法的作用是:当有消息推送到你手机上时 你在通知栏点击这个消息时,就能在这个方法中通过message获取 消息的内容。

第二 加入你想点击通知栏中的消息 跳转到你app中指定的界面 也在这个方法中执行 只需要添加一段代码即可:

Intent intent = new Intent(context, 指定的Activity.class); 
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  context.startActivity(intent); 

最后 我们要去配置AndroidManifest.xml
一些权限我就不放了 和以前的权限放一起了不好区分,可以去Demo中找
在权限下面放

<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/>中添加

<service 
   android:name="com.xiaomi.push.service.XMJobService" 
   android:enabled="true" 
   android:exported="false" 
   android:permission="android.permission.BIND_JOB_SERVICE" 
   android:process=":pushservice" /> 
 
  <service 
   android:name="com.xiaomi.push.service.XMPushService" 
   android:enabled="true" 
   android:process=":pushservice" /> 
 
  <service 
   android:name="com.xiaomi.mipush.sdk.PushMessageHandler" 
   android:enabled="true" 
   android:exported="true" /> 
  <service 
   android:name="com.xiaomi.mipush.sdk.MessageHandleService" 
   android:enabled="true" /> 
 
  <receiver 
   android: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> 
  <receiver 
   android: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> 
  <receiver 
   android: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> 

就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。