iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 19
1
Cloud

Xamarin.Forms, ASP.NET Core WEB API搭配AZURE服務與資料庫服務整合應用實例系列 第 19

Day19 - Azure Mobile App & Azure Notification Hubs Part2 Android篇

要能對Android發Push Message, 首先要註冊Firebase Cloud Messaging (FCM), 到下列網址, 點擊建立新專案
https://console.firebase.google.com/?pli=1
http://ithelp.ithome.com.tw/upload/images/20161229/20103333LE4WJ0eh4t.png

之後填入專案名稱
http://ithelp.ithome.com.tw/upload/images/20161229/20103333QtBIG8U2EQ.png

在總覽裡, 點選將Firebase加入您的Android應用程式
http://ithelp.ithome.com.tw/upload/images/20161229/20103333cjW7VWBe45.png

填入套件名稱, 並記下等到Visual Studio開發Android專案時會用到, 完成之後按下新增應用程式
http://ithelp.ithome.com.tw/upload/images/20161229/20103333ckujQ1H8cF.png

接著按下繼續
http://ithelp.ithome.com.tw/upload/images/20161229/20103333bGiQf8564U.png

最後點選完成
http://ithelp.ithome.com.tw/upload/images/20161229/20103333IoCGt6wHlw.png

再來點選專案設定, 按下CLOUD MESSAGING
http://ithelp.ithome.com.tw/upload/images/20161229/20103333GFKKveVdHa.png

之後會看到伺服器金鑰與寄件者ID, 這兩個都要記下, 之後會用到
http://ithelp.ithome.com.tw/upload/images/20161229/20103333YITdHV5xlN.png

回到Azure Portal內的推播通知服務, 點選Google(GCM), 填入剛剛取得的伺服器金鑰, 按下儲存
http://ithelp.ithome.com.tw/upload/images/20161229/20103333JiiZ7lQfAY.png

回到Visual Studio的Android專案, 在Components按下右鍵, 點選Get More Components
http://ithelp.ithome.com.tw/upload/images/20161229/20103333gy8tscdZWD.png

搜尋Google Cloud Messaging Client
http://ithelp.ithome.com.tw/upload/images/20161229/20103333sALzC3rn1E.png

按下Add to App
http://ithelp.ithome.com.tw/upload/images/20161229/20103333LJj5EFSu2A.png

接著在Android專案屬性的Android Manifest, 在Package name填入Firebase Cloud Messaging建立的套件名稱, 這個一定要填對, 不然無法正確部署
http://ithelp.ithome.com.tw/upload/images/20161229/20103333fP0LJhK2jQ.png

接著開始來寫程式了, 在MainActivity.cs加入Gcm.Client的參考
using Gcm.Client;
http://ithelp.ithome.com.tw/upload/images/20161229/20103333ue5UXordtv.png

在OnCreate函式,加入底下紅框程式碼
http://ithelp.ithome.com.tw/upload/images/20161229/20103333IziJtaygke.png

新增一個CreateAndShowDialog函式
http://ithelp.ithome.com.tw/upload/images/20161229/20103333u9TT06xaEO.png

接著加入一個靜態的MainActivity宣告, 並放置到OnCreate函式一開始的地方
http://ithelp.ithome.com.tw/upload/images/20161229/20103333GVSRECM5Y5.png

再來跟Windows Phone專案一樣, 新增Microsoft.Azure.Mobile.Client到Android裡
http://ithelp.ithome.com.tw/upload/images/20161229/20103333JM1SPOS5jk.png

接著新增一個GcmService.cs, 其程式碼如下, 有一個重點是在SENDER_IDS, 要填入在Firebase Cloud Messaging取得的寄件者ID如下紅框所示
http://ithelp.ithome.com.tw/upload/images/20161229/20103333sDJNl8L5lc.png
http://ithelp.ithome.com.tw/upload/images/20161229/20103333cvfeXI23oj.png
http://ithelp.ithome.com.tw/upload/images/20161229/20103333WRZdOXn5QN.png
http://ithelp.ithome.com.tw/upload/images/20161229/20103333rA2xSIlL0E.png
http://ithelp.ithome.com.tw/upload/images/20161229/201033332tG9XlsWCN.png
http://ithelp.ithome.com.tw/upload/images/20161229/20103333KantG3JLXS.png

完成之後, 就可以來測試看看, 在此不建議用模擬器這個功能, 試了幾個模擬器, 都有問題, 不想花時間除錯, 直接拿實機測試, 一樣到Azure測試頁面, 選擇Android, 按下傳送
http://ithelp.ithome.com.tw/upload/images/20161229/20103333WTdnLnL68D.png

成功畫面如下, 實機測試時要先登入Google帳號, 這個是雷區啊
http://ithelp.ithome.com.tw/upload/images/20161229/20103333smnxFGMHD5.png
http://ithelp.ithome.com.tw/upload/images/20161229/20103333UqAeEa8B9T.png

後記: Android設定挺簡單的, Code跟測試還真繁瑣, 果然世上有一好沒倆好啊, 接下來又是IOS大魔王了/images/emoticon/emoticon46.gif


上一篇
Day18 - Azure Mobile App & Azure Notification Hubs Part1 Windows篇
下一篇
Day20 - Azure Mobile App & Azure Notification Hubs Part3 IOS篇
系列文
Xamarin.Forms, ASP.NET Core WEB API搭配AZURE服務與資料庫服務整合應用實例32
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言