Android小米推送简单使用方法

所属分类: 软件编程 / Android 阅读数: 56
收藏 0 赞 0 分享

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

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


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.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> 

就可以了。

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

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多