NewGen
ios swift project , Azure notification hub 구현 본문
푸시 노티용 인증서는 이미 만들었다는 가정하에...
1. pod 설치
프로젝트의 podfile에 아래 추가.
pod 'AzureNotificationHubs-iOS'
pod install 하면 pod 설치됨.
2. capabilities 추가
3. appdelegate.swift 에
MSNotificationHubDelegate 추가
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MSNotificationHubDelegate {
import WindowsAzureMessaging
import UserNotifications
//Hyunny_noti
private var notificationPresentationCompletionHandler: Any?
private var notificationResponseCompletionHandler: Any?
private var connectionString: String?
private var hubName: String?
//delegate 함수 추가
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//푸시 사용여부 설정
let noti_center = UNUserNotificationCenter.current()
noti_center.getNotificationSettings() {
(settings) in
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) {
(granted,error) in
guard granted else {
//푸시 사용
self.send_push_key_to_server(strKey: "") { result in
//사용자가 푸시 안쓸경우
}
return
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
//azure
if let path = Bundle.main.path(forResource: "file_name_for_noti_hub", ofType: "plist") {
if let configValues = NSDictionary(contentsOfFile: path) {
self.connectionString = configValues["CONNECTION_STRING"] as? String
self.hubName = configValues["HUB_NAME"] as? String
if (!(self.connectionString ?? "").isEmpty && !(self.hubName ?? "").isEmpty)
{
UNUserNotificationCenter.current().delegate = self;
MSNotificationHub.setDelegate(self)
MSNotificationHub.start(connectionString: self.connectionString!, hubName: self.hubName!)
self.addTags()
//Azure noti hub start
}
}
}
}
}
}
.....
노티오면 메세지 파싱내용 잡을곳 함수 만들어 주고..
func notificationHub(_ notificationHub: MSNotificationHub!, didReceivePushNotification message: MSNotificationHubMessage!) {
let userInfo = ["message": message!]
NotificationCenter.default.post(name: NSNotification.Name("MessageReceived"), object: nil, userInfo: userInfo)
if (UIApplication.shared.applicationState == .background) {
NSLog("Notification received in the background")
}
if (notificationResponseCompletionHandler != nil) {
NSLog("Tapped Notification")
} else {
NSLog("Notification received in the foreground")
}
// Call notification completion handlers.
if (notificationResponseCompletionHandler != nil) {
(notificationResponseCompletionHandler as! () -> Void)()
notificationResponseCompletionHandler = nil
}
if (notificationPresentationCompletionHandler != nil) {
(notificationPresentationCompletionHandler as! (UNNotificationPresentationOptions) -> Void)([])
notificationPresentationCompletionHandler = nil
}
}
4. file_name_for_noti_hub.plist 를 추가해주고 필드를 각각
CONNECTION_STRING : String
HUB_NAME : String
와 같이 추가.
저기 들어가는 값은
HUB_NAME : 애져 노티허브에 만들어준 허브명
CONNECTION_STRING : DefaultListenSharedAccessSignature 값을 넣어주면 됨.
위 값은 허브 관리 사이트에서, Access Polices 에서 확인 가능.
5. 만들어논 허브 선택해서 좌측에서 테스트 센드 선택하고,
6.요래 보내면 된다
참고
공식문서이고, 여기 잘 보면, 인증서 만드는 설명도 상세하게 나옴.
docs.microsoft.com/ko-kr/azure/notification-hubs/ios-sdk-get-started
'IOS' 카테고리의 다른 글
Background Task 2 (0) | 2020.10.27 |
---|---|
swift background 처리 (0) | 2020.10.22 |
Download mp3 from youtube on your MAC. (0) | 2020.05.31 |
Install homebrew on OSX (0) | 2020.05.31 |
GoGo Lotto 사용자 매뉴얼 (0) | 2020.04.22 |