封裝使用 aws 的 simple notification service
由於目前 aws 官方上面的文件講解不太完全, 且也沒太多相關的教學以及經驗分享,
在接入 simple notification service 推播服務時遇到不小的困難,
因此這邊整理了所有的步驟, 並且封裝成lib方便接入
接入方式參考:
@thabodavidnyakalloklass/ios-push-with-amazons-aws-simple-notifications-service-sns-and-swift-made-easy-51d6c79bc206">iOS Push with Amazon’s AWS Simple Notifications Service (SNS)
1.0.0 - 更新 swift version 至 4.2, 將註冊主題的部分拉出, 可由外部呼叫是否註冊主題
0.0.2 - 修改顯示名稱
0.0.1 - 初始專案提交
pod ‘MGAwsSnsManagerSwift’, ‘~> {version}’
( 其中 {version} 請自行替入此版號 )
專案引入 aws sdk, 可使用 cocoapods 直接引入, 當前文件使用的 sdk 版本皆為 2.6.29
pod 'AWSCore', '~> 2.6.31'
pod 'AWSSNS', '~> 2.6.31'
pod 'AWSCognito', '~> 2.6.31'
AppDelegate.swift 加入如下
import MGAwsSnsManagerSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
//aws sns推播服務封裝類別
private var mAwsManager: MGAwsSnsManager
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//推播類初始化配置
do {
//有兩種初始化設置的方式, 選擇一個進行初始化
//a. 無參數 - 須先在專案目錄下創建並加入名為 mgawssnsconfig.txt 的文件, 內容可參考最下方說明, 沒有或者內容不對則拋出錯誤
MGAwsSnsManager.shared.loadConfig()
//b. 有參數 - 帶入需要的資料
MGAwsSnsManager.shared.loadConfig(
applicationArn: "",
topicsArn: [],
region: "",
identityPoolId: ""
)
//最後開始依照配置檔進行向aws註冊app推播, 此步驟會設定 aws 身份憑證, 並且向系統註冊遠程推播
try MGAwsSnsManager.shared.settingStart()
} catch {
print("初始化 awsManager 設置出現錯誤 \(error)")
}
return true
}
}
//當向系統註冊遠程推播成功後調用
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//註冊 app token 到 aws app arn 下, 同時註冊 topic
//@param autoRegisterTopic - 是否在app註冊成功後自動註冊主題
mAwsManager.registerToApplication(deviceToken: deviceToken, autoRegisterTopic: false)
}
mgawssnsconfig.txt 的內容格式: Config配置
訂閱/取消訂閱/檢查訂閱 主題
//訂閱主題
MGAwsSnsManager.shared.registerToTopic()
//取消訂閱主題
MGAwsSnsManager.shared.unregisterTopic()
//檢查主題是否已訂閱
MGAwsSnsManager.shared.isTopicRegistered
至此 aws 配置完成, 可以執行app看看了(當然不能是模擬器, 要實機)
若一切無誤的話, 可以在 sns app arn 點進去之後可以看到註冊的裝置, topic 同理