iOS 10 以上通过 userNotification.framework 请求授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
switch settings.authorizationStatus {
case .authorized:
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
case .notDetermined:
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print("Notification authorization failed. ERROR: \(error?.localizedDescription ?? "")")
}
}
default:
break
}
}

iOS7 及之前版本

1
2
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

iOS7 推送教程

iOS 7 和 iOS 8 推送教程

iOS 8 - iOS 10

1
2