iOS集成AWS Push整理

时间:2021-03-07 18:41:00

#iOS集成AWS整理PushNotification

##集成AWS

###AWS官网连接
案例中是Swift版本,Demo提供了OC版本的集成
1.Profile 中配置AWS SDK

1
2
3
4
5
6
7
8
9
platform :ios, '9.0'

target :'YOUR-APP-NAME' do
use_frameworks!

pod 'AWSPinpoint', '~> 2.9.0'
# other pods
pod 'AWSMobileClient', '~> 2.9.0'
end

运行

1
2
3
4
pod install --repo-update 
```

2.导入头文件在AppDelegate中:

import AWSCore
import AWSPinpoint
import AWSMobileClient

1
3.初始化,连接AWS服务器,创建Pinpoint对象

lass AppDelegate: UIResponder, UIApplicationDelegate {

/** start code copy **/
var pinpoint: AWSPinpoint?
/** end code copy **/

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

     // Other didFinishLaunching code...

     /** start code copy **/
     // Create AWSMobileClient to connect with AWS
 AWSMobileClient.sharedInstance().initialize { (userState, error) in
       if let error = error {
         print("Error initializing AWSMobileClient: (error.localizedDescription)")
       } else if let userState = userState {
         print("AWSMobileClient initialized. Current UserState: (userState.rawValue)")
       }
     }

 // Initialize Pinpoint
     let pinpointConfiguration = AWSPinpointConfiguration.defaultPinpointConfiguration(launchOptions: launchOptions)
     pinpoint = AWSPinpoint(configuration: pinpointConfiguration)
     /** end code copy **/
 return true
}

}

1
2
3
4
5
初始化会访问本地awsconfiguration.json文件,成功 userState 返回 guest。

###获取awsconfiguration.json
[amplify 集成](https://aws-amplify.github.io/docs/cli/init?sdk=ios)
执行命令

amplify init
amplify push

1
2
3
生成awsconfiguration.json放入项目中
###上传token
使用创建好的pinpoint上传token

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    pinpoint!.notificationManager.interceptDidRegisterForRemoteNotifications(
        withDeviceToken: deviceToken)
}

```
AWS后台会通过device token生成Endpoint ID,功能相同

AWSMobileClient库 部分代码使用Swift编写,如果是使用OC编写的项目,需要进行代码混编的集成

iOS集成AWS Push整理
iOS集成AWS Push整理

Download Demo

原文:大专栏  iOS集成AWS Push整理