当前位置: 移动技术网 > IT编程>移动开发>Android > Android小米推送简单使用方法

Android小米推送简单使用方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

亲吻前 结婚后,云雀ghk7070,fedex国内快递查询

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

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> 

就可以了。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网