Android小米推送簡(jiǎn)單使用方法
公司項(xiàng)目需要做推送,我們選擇用小米推送,經(jīng)過一段時(shí)間的摸索,終于可以簡(jiǎn)單的使用小米推送了。
1.創(chuàng)建賬號(hào)登入后 登入后選擇消息推送:
2.進(jìn)入后創(chuàng)建項(xiàng)目,按照步驟創(chuàng)建完后如下
3.后臺(tái)配置完了,我們?cè)倥渲么a,第一次使用小米推送 我下了個(gè)Demo再把里面有用的復(fù)制到自己項(xiàng)目中:
先把jar包復(fù)制到自己項(xiàng)目中
首先在繼承了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什么的都是在小米開放平臺(tái)創(chuàng)建項(xiàng)目獲得的
再在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(); } } }
說實(shí)話Demohander其實(shí)沒什么用,主要是彈出toast提示而已,我不喜歡 于是隱藏了toast
其中MainActivity中的refreshLogInfo()方法:
public void refreshLogInfo() { String AllLog = ""; for (String log : logList) { AllLog = AllLog + log + "\n\n"; } System.out.println("mainActivity中消息推送::::::::"+AllLog); }
然后 我們要把Demo中的一個(gè)廣播類復(fù)制過來(lái) ,由于內(nèi)容一樣我就不復(fù)制了
其中有個(gè)方法很重要: onNotificationMessageClicked(Context context, MiPushMessage message)
這個(gè)方法的作用是:當(dāng)有消息推送到你手機(jī)上時(shí) 你在通知欄點(diǎn)擊這個(gè)消息時(shí),就能在這個(gè)方法中通過message獲取 消息的內(nèi)容。
第二 加入你想點(diǎn)擊通知欄中的消息 跳轉(zhuǎn)到你app中指定的界面 也在這個(gè)方法中執(zhí)行 只需要添加一段代碼即可:
Intent intent = new Intent(context, 指定的Activity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);
最后 我們要去配置AndroidManifest.xml
一些權(quán)限我就不放了 和以前的權(quán)限放一起了不好區(qū)分,可以去Demo中找
在權(quán)限下面放
<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>
就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android消息推送:手把手教你集成小米推送(附demo)
- Android手機(jī)端小米推送Demo解析和實(shí)現(xiàn)方法
- Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
- Android中使用WebSocket實(shí)現(xiàn)群聊和消息推送功能(不使用WebView)
- 使用SignalR推送服務(wù)在Android的實(shí)現(xiàn) SignalA
- Android中使用socket通信實(shí)現(xiàn)消息推送的方法詳解
- Erlang實(shí)現(xiàn)的百度云推送Android服務(wù)端實(shí)例
- Android頂欄定時(shí)推送消息
- android push推送相關(guān)基本問答總結(jié)
- Android、iOS和Windows Phone中的推送技術(shù)詳解
相關(guān)文章
Android仿硅谷商城實(shí)現(xiàn)購(gòu)物車實(shí)例代碼
這篇文章主要介紹了Android購(gòu)物車編輯實(shí)現(xiàn),小編覺得挺不錯(cuò)的,一起跟隨小編過來(lái)看看吧2018-05-05使用Fragment來(lái)處理Andoird app的UI布局的實(shí)例分享
這篇文章主要介紹了使用Fragment來(lái)處理Andoird appUI布局的實(shí)例分享,Fragment的出現(xiàn)緩解了代碼依賴于Activity而造成的臃腫狀況,需要的朋友可以參考下2016-02-02ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡(jiǎn)單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06Android編程實(shí)現(xiàn)手機(jī)拍照的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)手機(jī)拍照的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)手機(jī)拍照的操作步驟與具體細(xì)節(jié),需要的朋友可以參考下2016-11-11修改Android簽名證書keystore的密碼、別名alias以及別名密碼
這篇文章主要介紹了修改Android簽名證書keystore的密碼、別名alias以及別名密碼的相關(guān)資料,需要的朋友可以參考下2015-12-12Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法
這篇文章主要介紹了Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法,涉及Android操作多媒體文件及系統(tǒng)硬件設(shè)備的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android對(duì)圖片Drawable實(shí)現(xiàn)變色示例代碼
這篇文章主要給大家介紹了關(guān)于Android對(duì)圖片Drawable實(shí)現(xiàn)變色的相關(guān)資料,文中通過示例代碼將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08