ユーザーがプッシュ通知のためにあなたのアプリケーションの許可を与えられている/許可している場合にのみ、それぞれの起動時にデバイストークンを受け取ることができます。また、これは通常、AppDelegateで簡単に取り込むことができます。
ここで私の例では、UserDefaults(あまり推奨されません)とANDのコアデータを保存します(アプリケーションで有効にした場合、ローカル暗号化を利用できるため推奨します)。
あなたは、iOS上で10 +を動作するように意図されており、起動時にプッシュ通知を要求しているアプリケーションのために、そのように承認を要求する..:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
notificationHandler(application)
return true
}
func notificationHandler(_ application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings(){ (settings) in
switch settings.authorizationStatus {
case .authorized:
print("Authorized Push Notifications by User")
self.registerPushNotifications()
case .denied:
print("show user a view explaining why it's better to enable")
self.registerPushNotifications()
case .notDetermined:
self.requestPushNotifications(center: center, { (granted) in
if granted {
self.registerPushNotifications()
return
}
print("User did not grant Remote Notifications Authorizations")
})
}
}
} else {
print("App does not meet minimum OS Requirements")
return
}
}
fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) {
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if error != nil {
print("Could not request Authorization for Notifications - Yet is undetermined")
} else {
result(granted)
}
}
}
func registerPushNotifications() {
if #available(iOS 10.0, *) {
UIApplication.shared.registerForRemoteNotifications()
} else {
print("App does not meet base OS requirements")
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken")
coreDataManager.saveDeviceToken(deviceTokenString)
}
}
を最終的に、あなただけでこれを要求する場合それはとにかく、すべてのコントローラー型クラスのルートクラスであるAppDelegate
とUIViewController
(両方のスーパークラスですので、あなたのアプリケーション内の他の瞬間には、あなたは、NSObject
を拡張することができます。
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
notificationHandler(application)
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken")
coreDataManager.saveDeviceToken(deviceTokenString)
}
}
extension NSObject {
func notificationHandler(_ application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings(){ (settings) in
switch settings.authorizationStatus {
case .authorized:
print("Authorized Push Notifications by User")
self.registerPushNotifications()
case .denied:
print("show user a view explaining why it's better to enable")
self.registerPushNotifications()
case .notDetermined:
self.requestPushNotifications(center: center, { (granted) in
if granted {
self.registerPushNotifications()
return
}
print("User did not grant Remote Notifications Authorizations")
})
}
}
} else {
print("App does not meet minimum OS Requirements")
return
}
}
fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) {
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if error != nil {
print("Could not request Authorization for Notifications - Yet is undetermined")
} else {
result(granted)
}
}
}
func registerPushNotifications() {
if #available(iOS 10.0, *) {
UIApplication.shared.registerForRemoteNotifications()
} else {
print("App does not meet base OS requirements")
}
}
}
あなたは、ボタンの内側にこれを呼び出すことができますリンクされたメソッド:
class SomeController : UIViewController {
override viewDidLoad() {
super.viewDidLoad()
}
func requestNotificationsAuthorization() {
notificationHandler(UIApplication.current)
}
}
達成したい結果を明確にすることはできますか? –
質問を再投稿しないでください。あなたの[前の質問](https://stackoverflow.com/questions/45459888/how-to-present-apns-token-in-view-controller)に重複しています。リンクされた質問には、デバイストークンを文字列として取得する方法がたくさんあります。これはSwift 4でもおそらく動作します。それ以外に、あなたが達成しようとしていることは明確ではありません。 – vadian
アクセストークンをユーザーのデフォルトに格納できます。 – Jaydeep