๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/API] OneSignal์œผ๋กœ ํ‘ธ์‰ฌ์•Œ๋ฆผ ๋ณด๋‚ด๋ณด๊ธฐ(OneSignal Push Notification)

by Fomagran ๐Ÿ’ป 2021. 3. 24.
728x90
๋ฐ˜์‘ํ˜•

 

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ์›์‹œ๊ทธ๋„์ด๋ผ๋Š” ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•ด ํ‘ธ์‰ฌ์•Œ๋ฆผ์„ ๊ตฌํ˜„ํ•ด๋ณผ๊ฒŒ์š”!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~


KeyChainAccess

 

๋จผ์ € ํ‚ค์ฒด์ธ ์ ‘๊ทผ์œผ๋กœ ๊ฐ€์„œ ์ธ์ฆ ๊ธฐ๊ด€์—์„œ ์ธ์ฆ์„œ๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

 

์ž์‹ ์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋””์Šคํฌ์— ์ €์žฅ๋จ์„ ํด๋ฆญํ•œ ๋’ค ๊ณ„์†์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

์ธ์ฆ์„œ๋ฅผ ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ง€์€ ๋‹ค์Œ ์ €์žฅํ•ด์ฃผ์„ธ์š”!

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผ์ด ์ƒ๊ธธ๊ฑฐ์—์š”!


Apple Developer

 

์ด์ œ ์• ํ”Œ ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

 

๋กœ๊ทธ์ธ - Apple

 

idmsa.apple.com

Certificates,Identifiers & Profiles๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”.

 

 

์™ผ์ชฝ ํƒญ์—์„œ Identifiers๋ฅผ ํด๋ฆญํ•ด์ฃผ์‹œ๊ณ  Identifiers + ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

 

App IDs๋ฅผ ํด๋ฆญํ•ด์ฃผ์‹œ๊ณ  Continue๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

 

App์„ ๋ˆŒ๋Ÿฌ์ฃผ์‹œ๊ณ  Continue๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

์›ํ•˜๋Š” ์„ค๋ช…๊ณผ ํ”„๋กœ์ ํŠธ์˜ Bundle ID๋ฅผ ์ ์–ด์ค€ ๋’ค ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๋ฉด

 

์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋Š”๋ฐ ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ๊ฑด ํ‘ธ์‰ฌ ์•Œ๋ฆผ์ด๋‹ˆ๊น Push Notifications ๋งŒ ๋ˆ„๋ฅด๊ณ  Configure๋ฅผ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

 

๋งˆ์ง€๋ง‰์œผ๋กœ Register๋ฅผ ๋ˆŒ๋Ÿฌ ์•ฑ์„ ๋“ฑ๋ก์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

์ด์ œ ๋“ฑ๋ก๋œ ์•ฑ์„ ํด๋ฆญํ•ด์ฃผ์„ธ์š”.

 

 

๋ฐ‘์œผ๋กœ ์ญ‰ ๋‚ด๋ฆฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Push Notifications ์˜†์— Edit ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

์œ„์— ์œ„์น˜ํ•ด์žˆ๋Š” Development SSL Certificate๋ฅผ Create ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋œฐํ…๋ฐ Platform์€ iOS๋กœ ํ•ด์ฃผ์‹œ๊ณ  ๋ฐ‘์— Choose File์— ์œ„์—์„œ ๋‹ค์šด๋ฐ›์•„๋†“์€ ์ธ์ฆ์„œ๋ฅผ ๊ณจ๋ผ์ค๋‹ˆ๋‹ค.

 

 

๊ทธ ๋‹ค์Œ Continue๋ฅผ ๋ˆŒ๋Ÿฌ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€์ฃผ์‹œ๊ณ 

 

๋งˆ์ง€๋ง‰์œผ๋กœ Download๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ๋ฅผ ํ•ด์ฃผ์„ธ์š”.

 

๊ทธ ๋‹ค์Œ ํ‚ค์ฒด์ธ ์•ก์„ธ์Šค๋กœ ๊ฐ€๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Development ์ธ์ฆ์„œ๊ฐ€ ์ƒˆ๋กœ ์ƒ๊ฒผ์„ํ…๋ฐ ์—ฌ๊ธฐ์„œ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•œ ๋’ค

 

๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

 

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ธ์ฆ์„œ ํŒŒ์ผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์•„๋ž˜ ํŒŒ์ผ์€ .p12๋กœ ์„ ํƒํ•ด์ฃผ์‹œ๊ณ  ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค. (์ด๋ฆ„์€ ์›ํ•˜์‹œ๋Š”๊ฑฐ ์“ฐ์…”๋„ ๋ผ์š”!)

ํŒŒ์ผ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ •ํ•ด์ค€ ๋’ค ํ™•์ธ์„ ๋ˆ„๋ฅด๊ณ 

๋งฅ์—์„œ ๋กœ๊ทธ์ธํ• ๋•Œ ์ž…๋ ฅํ•˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.

 

์ด๋ ‡๊ฒŒ ์ €์žฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด p12 ํŒŒ์ผ์ด ์ €์žฅ๋ ๊ฑฐ์—์š”!

 

์ด์ œ ๋‹ค์‹œ ์• ํ”Œ ๋””๋ฒจ๋กœํผ ์‚ฌ์ดํŠธ๋กœ ๊ฐ€์„œ ์•„๊นŒ Push Notifications Edit ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

๊ฑฐ๊ธฐ์„œ ์ด์ œ ์•„๋ž˜ Production SSL Certificate๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.

 ๋˜‘๊ฐ™์ด Choose File์„ ๋ˆ„๋ฅด๊ณ  ์•„๊นŒ์™€ ๊ฐ™์ด ์ธ์ฆ์„œ๋ฅผ ๋ˆ„๋ฅธ๋’ค ๋‹ค์šด๋กœ๋“œ ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ‚ค์ฒด์ธ ์•ก์„ธ์Šค๋กœ ๊ฐ€๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด Development ์ธ์ฆ์„œ๊ฐ€ ์•„๋‹Œ Apple Push Services ์ธ์ฆ์„œ๊ฐ€ ์žˆ์„๊ฑฐ์—์š” 

 

์—ฌ๊ธฐ์„œ ๋‚ด๋ณด๋‚ด๊ธฐ ํ•ด์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Development ์ธ์ฆ์„œ์™€ ํ—ท๊ฐˆ๋ฆฌ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ๋’ค์— Prod๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด p12์ธ์ฆ์„œ ๋‘๊ฐœ๊ฐ€ ์žˆ์„๊ฑฐ์—์š”.

 

์ด๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ์šฐ์„  1์ฐจ ์„ธํŒ…์€ ๋ชจ๋‘ ๋๋‚ด์‹ ๊ฒ๋‹ˆ๋‹ค!!

 


OneSignal

 

์ด์ œ OneSignal์œผ๋กœ ์ด๋™ํ•ด์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•ด์ฃผ์„ธ์š”!

 

Customer Messaging Delivered | Send Mobile & Web Push Notifications,…

The world's leader for mobile push notifications, web push, SMS, email & in-app messaging. Trusted by 1 million+ developers to send billions of message per day.

onesignal.com

๊ทธ๋ฆฌ๊ณค New App/Website ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์•ฑ์ด๋ฆ„์„ ์ •ํ•˜๊ณ  Apple iOS๋ฅผ ํƒญํ•ด์ฃผ์‹œ๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„  ์•„๋ž˜ Choose File์ด ๋ณด์ผ๊ฑฐ์—์š”. 

 

๋ˆŒ๋Ÿฌ์ฃผ์‹œ๊ณ  ์•„๊นŒ ๋‹ค์šด๋ฐ›์•„๋†“์€ p12 ๋‘๊ฐœ์˜ ํŒŒ์ผ์ค‘ Prod๋ผ๊ณ  ์ด๋ฆ„ ๋’ค์— ๋ถ™์ธ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  p12 ํŒŒ์ผ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํ›„ Next๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”!

 

๊ทธ ๋‹ค์Œ Native iOS๋ฅผ ํด๋ฆญํ•ด์ฃผ์‹œ๊ณ  Next๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

์—ฌ๊ธฐ์„œ ์•ฑ ์•„์ด๋””๊ฐ€ ๋ฐœ๊ธ‰๋˜๋Š”๋ฐ ์ž˜ ์ €์žฅํ•ด์ฃผ์„ธ์š”!

 

๊ทธ๋ฆฌ๊ณค save ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.


Xcode

 

ํ”„๋กœ์ ํŠธ์˜ TARGETS๋กœ ์ด๋™ํ•ด์„œ Signing & Capabilities๋กœ ์ด๋™ํ•˜์‹ ๋’ค ์•„๋ž˜์— ์žˆ๋Š” +Capbility ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ

Background Modes์™€ Push Notifications๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!

 

๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ชจ๋“œ์—์„œ๋Š” Remote notifications๋ฅผ ํด๋ฆญํ•ด์ฃผ์„ธ์š”.

๊ทธ ๋‹ค์Œ podํŒŒ์ผ๋กœ ์ด๋™ํ•ด์„œ OneSignal์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.


Podfile

 

  pod 'OneSignal'

AppDelegate

 

์•ฑ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋กœ ์ด๋™ํ•ด์„œ ๋จผ์ € OneSignal์„ import ํ•ด์ค๋‹ˆ๋‹ค.

 

import OneSignal

 

OSSubscriptionObserver๋ฅผ ์ฑ„ํƒํ•ด์ฃผ์„ธ์š”.

 

@main
class AppDelegate: UIResponder, UIApplicationDelegate,OSSubscriptionObserver{
... }

 

์œ„ ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ฑ„ํƒํ•˜์‹œ๋ฉด ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ• ๊ฑฐ์—์š”!

    func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges) {...}

๊ทธ๋ฆฌ๊ณค didFinishLaunchingWithOptions ๋ฉ”์†Œ๋“œ๋กœ ์ด๋™ํ•˜์…”์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ธํŒ…ํ•ด์ฃผ์„ธ์š”.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
	
       	requestNotificationPermission()
        OneSignal.add(self as OSSubscriptionObserver)
        OneSignal.initWithLaunchOptions(launchOptions)
        OneSignal.setAppId("์›์‹œ๊ทธ๋„์•ฑ์•„์ด๋””")
        
        ...
        
        }

requestNotificationPermission ๋ฉ”์†Œ๋“œ๋ฅผ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

   func requestNotificationPermission(){
           UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in
               if didAllow {
                   print("Push: ๊ถŒํ•œ ํ—ˆ์šฉ")
               } else {
                   print("Push: ๊ถŒํ•œ ๊ฑฐ๋ถ€")
               }
           })
       }

๊ทธ ๋‹ค์Œ ์•ฑ์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š”๋ฐ์š”.

 

์ฃผ์˜ํ•˜์‹ค ์ ์€ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ์‹ค์ œ ์•„์ดํฐ ๋””๋ฐ”์ด์Šค๋กœ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. (์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ์—์„  ํ‘ธ์‰ฌ์•Œ๋ฆผ์ด ์•ˆ๋ฉ๋‹ˆ๋‹ค....)

 

๊ทธ ๋‹ค์Œ์œผ๋ก  ์•„๋ž˜ ํ•จ์ˆ˜๋ฅผ didFinishLaunchingWithOptions ๋ฐ‘์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

userID๋Š” ํ˜„์žฌ ๋””๋ฐ”์ด์Šค๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ์˜ ์•„์ด๋””๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

   OneSignal.promptForPushNotifications(userResponse: { accepted in
            
            let userID = OneSignal.getDeviceState()!.userId
            
            OneSignal.postNotification(["contents":["en":"์•ˆ๋…•ํ•˜์„ธ์š”!"],"ios_badgeType":"Increase","ios_badgeCount":"1","include_player_ids":[userID ?? ""]])
    })

 

์ด๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘ธ์‰ฌ์•Œ๋žŒ๊ณผ ๋ฑƒ์ง€์ƒ์„ฑ์ด ๋˜๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!

 


 

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ OneSignal์„ ์ด์šฉํ•ด์„œ ํ‘ธ์‰ฌ์•Œ๋žŒ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€