[Swift] iOS 애플리케이션에 알림 푸시 설치를 위한 샘플 애플리케이션을 만들어 봤습니다.
iOS의 푸시 알림은 이런 느낌이에요.
iOS 10의 푸시 알림 디자인은 이런 느낌이다.(조금 달라, iOS 13도 같은 느낌)
Swift에서 제작한 iOS 애플리케이션에 대해 알림을 실시하고 싶습니다!나는 이런 사람에게 이 문장을 참고할 수 있다고 생각한다![:thumbsup:](https://s1.md5.ltd/image/2eeeb9d8d715cd46af8f2d2afebdf285.png)
견본
작업 환경 및 사전 준비
동작 환경 추천(동작 확인을 하는 환경입니다)
작업 환경 및 사전 준비
동작 환경 추천(동작 확인을 하는 환경입니다)
사전 준비
느릅나무의 계정 취득(SNS ID를 통해 취득: 무료)
견본 항목 일람
모든 것을 자유롭게 사용할 수 있다
언어
알림 푸시
부압
유효 하중 취득
Swift
SwiftPushApp
SwiftRichPushApp
SwiftPaylpadApp
코드 소개
원래 알림을 푸시하는 장치는?
iOS의 경우 애플의 푸시 알림을 통해 서버로 푸시 알림을 보냅니다.
다음 그림에서 보듯이 ①~⑤의 절차에 따라 알림을 추진하면 단말기로 전송된다.
여기서 ② 장치 토큰이 발행될 때 APNs에서 발행된 장치 토큰은 터미널에서 가져와야 하며 서버에 저장된 처리는 응용 프로그램에서 이루어져야 한다.
<Push 알림의 기본> 장치 토큰의 수령 및 서버 등록
AppDelegateimport UIKit
import UserNotifications
import NCMB
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//********** APIキーの設定 **********
let applicationkey = "YOUR_NCMB_APPLICATIONKEY"
let clientkey = "YOUR_NCMB_CLIENTKEY"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//********** SDKの初期化 **********
NCMB.initialize(applicationKey: applicationkey, clientKey: clientkey)
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) {granted, error in
if error != nil {
// エラー時の処理
return
}
if granted {
// デバイストークンの要求
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}
// デバイストークンが取得されたら呼び出されるメソッド
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 端末情報を扱うNCMBInstallationのインスタンスを作成
let installation : NCMBInstallation = NCMBInstallation.currentInstallation
// デバイストークンの設定
installation.setDeviceTokenFromData(data: deviceToken)
// 端末情報をデータストアに登録
installation.saveInBackground {result in
switch result {
case .success:
// 端末情報の登録に成功した時の処理
break
case let .failure(error):
// 端末情報の登録に失敗した時の処理
print(error)
break
}
}
}
}
부압 처리
부압이 뭐예요?
import UIKit
import UserNotifications
import NCMB
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//********** APIキーの設定 **********
let applicationkey = "YOUR_NCMB_APPLICATIONKEY"
let clientkey = "YOUR_NCMB_CLIENTKEY"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//********** SDKの初期化 **********
NCMB.initialize(applicationKey: applicationkey, clientKey: clientkey)
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) {granted, error in
if error != nil {
// エラー時の処理
return
}
if granted {
// デバイストークンの要求
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}
// デバイストークンが取得されたら呼び出されるメソッド
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 端末情報を扱うNCMBInstallationのインスタンスを作成
let installation : NCMBInstallation = NCMBInstallation.currentInstallation
// デバイストークンの設定
installation.setDeviceTokenFromData(data: deviceToken)
// 端末情報をデータストアに登録
installation.saveInBackground {result in
switch result {
case .success:
// 端末情報の登録に成功した時の処理
break
case let .failure(error):
// 端末情報の登録に失敗した時の処理
print(error)
break
}
}
}
}
![](https://s1.md5.ltd/image/ed7dbd30c0e622f71c88f4162cf60f49.png)
// MARK: アプリが起動されるときに実行される処理を追記する場所
if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject] {
NCMBPush.handleRichPush(userInfo: notification)
}
페이로드 처리
유효 하중은 무엇입니까?
![](https://s1.md5.ltd/image/8b9cae198a6ad4256ac63ed767407e1c.png)
프로그램이 시작되지 않을 때 수신
didFinishLaunchingWithOptions
방법에 기술// 【ペイロード:アプリ非起動時に受信】アプリが起動されたときにプッシュ通知の情報を取得する
if let remoteNotification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? NSDictionary {
/* 省略 */
}
프로그램이 시작될 때 받는 상황
didReceiveRemoteNotification
방법 및 기술// 【ペイロード:アプリ起動時に受信】アプリが起動中にプッシュ通知の情報を取得する
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
/* 省略 */
}
참고 자료
GiitHub에 많은 샘플 앱이 공개되었습니다!
https://github.com/natsumo/
Reference
이 문제에 관하여([Swift] iOS 애플리케이션에 알림 푸시 설치를 위한 샘플 애플리케이션을 만들어 봤습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/natsumo/items/ebba9664494ce64ca1b8
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여([Swift] iOS 애플리케이션에 알림 푸시 설치를 위한 샘플 애플리케이션을 만들어 봤습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/natsumo/items/ebba9664494ce64ca1b8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)