iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 16
0
Software Development

30 天上手 iOS App 開發系列 第 21

30 天上手 iOS App 開發 DAY 21

「如何使用Firebase Cloud Messaging在iOS實現推播的功能」(下篇)


使用 Google 帳號登入 Firebase 後台並建立專案

首先,先進入https://console.firebase.google.com/
登入後選擇建立專案,名稱就打上你的專案名稱吧(可自訂),專案ID不需更改,完成後點選建立專案。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506KQGvLs2ZuZ.png

將 Firebase 加入 iOS 的應用程式

https://ithelp.ithome.com.tw/upload/images/20180118/20107506M62ksznvQN.png

iOS繫結ID: 需對應Xcode專案的bundle identifier

https://ithelp.ithome.com.tw/upload/images/20180118/201075064s8QTGxyQ2.png

註冊後,下載GoogleService-info.plist,如果你已經有在Xcode建立專案,則直接拉到專案底下;如果還沒建立,那.plist檔先留著,先跟著我繼續做吧!

https://ithelp.ithome.com.tw/upload/images/20180118/201075065koaEC3hjy.png

點選Overview 旁的齒輪 → 專案設定 → Cloud Message → 上傳憑證 → 選擇上篇產出的 .p12 檔

  • 若在上篇時有設定密碼,此時會跳出視窗請您再輸入密碼。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506susuFyfLee.png

以上已完成Firebase的後台設定了,再來要在 Xcode 裡加入GoogleService-info.plist以及接收遠端通知的程式碼了。


建立一個有Firebase推播功能的App

創建專案相信你從第一天學到這邊已經會了,我這邊就教有關於推播的部分。

首先,要先整合Firebase SDK到你的專案,最簡單的方法就是使用CocoaPods。

關閉專案後,打開Terminal,打上專案路徑(或是按住紅色框的圖案,拖移至Terminal,即自動加上路徑)

https://ithelp.ithome.com.tw/upload/images/20180118/20107506GIoeQv3iP2.png

進入專案資料夾後,輸入「pod init」,此時你的專案資料夾底下會新增一個Podfile檔案。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506wQvoWxOilW.png

打開Podfile,加入:
pod 'Firebase/Core'
pod 'Firebase/Messaging'

https://ithelp.ithome.com.tw/upload/images/20180118/20107506P3ONtuWc7a.png

儲存後回到Terminal,輸入「pod install」。
CocoaPods會隨即下載SDK至你的專案並建立一個.xcworkspace。

接著把GoogleService-info.plist移致專案資料夾底下。
完成後,打開.xcworkspace檔來開啟專案。

啟動推播功能

打開 Xcode Push Notification 的選項
選擇專案 → TARGETS → Capabilities → ON

https://ithelp.ithome.com.tw/upload/images/20180118/20107506SXwwT0NMRm.png

進入AppDelegate.swift,import以下模組。

  • import Firebase
  • import UserNotifications

接著加入協定到你的AppDelegate class

UNUserNotificationCenterDelegate,MessagingDelegate

再來需要設定向使用者請求推播功能,iOS 10.0後有新方法,
故在application(_:didFinishLaunchingWithOptions:)輸入以下程式碼

if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
            
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
                        
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        
        application.registerForRemoteNotifications()
        FirebaseApp.configure()

FirebaseApp.configure()即為初始設定Firebase

接著基於MessagingDelegate協定下,實作以下方法。

func application(received remoteMessage: MessagingRemoteMessage) {
        print(remoteMessage.appData)
    }

再來就可以在你的裝置上執行專案了,在App詢問是否允許推播通知時,一定要點Allow才能接收到推播,如果誤點也可以去設定中更改。

https://ithelp.ithome.com.tw/upload/images/20180118/201075063PmPph5ffA.jpg


來傳送你第一個推播通知吧!

到Firebase後台,左邊列表找到Notifications

https://ithelp.ithome.com.tw/upload/images/20180118/20107506Up9dI9AEez.png

選擇「新增訊息」。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506rRyPpMNbBu.png

填入訊息內容以及選擇目標應用程式。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506IDM2xehUq1.png

如果要加入標題,往下找可以找到 進階選項->標題,完成後直接按傳送訊息。

https://ithelp.ithome.com.tw/upload/images/20180118/20107506a5FHTJTDwF.png

傳送!

https://ithelp.ithome.com.tw/upload/images/20180118/201075066IlqrjJVzH.png

過幾秒裝置就會跳出推播囉

https://ithelp.ithome.com.tw/upload/images/20180118/20107506osEni9dtGN.png


「如何使用Firebase Cloud Messaging在iOS實現推播的功能」就到這邊,明天見囉!


上一篇
30 天上手 iOS App 開發 DAY 20
下一篇
30 天上手 iOS App 開發 DAY 23
系列文
30 天上手 iOS App 開發28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言