我有这个代码注册app app上的推送通知,但什么也没发生。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
singleton.deviceToken = nil;
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = deviceToken.description.trimmingCharacters(in: CharacterSet.init(charactersIn:"<>"));
token = token.replacingOccurrences(of: " ", with: "");
singleton.deviceToken = token;
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types == .none { application.registerForRemoteNotifications() }
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Cannot register for push notification. Repeating.")
application.registerForRemoteNotifications();
}
application(application:didFinishLaunchingWithOptions:)
。所有其他人永远不会被召唤有什么问题?以及如何解决这个问题?至少,如果我没有注册推送通知,我希望在控制台上重复打印Cannot register for push notification. Repeating.
。但是控制台上也没有任何东西。
PS:我刚刚将我的Xcode更新为8.2.1。但话说回来,在我升级到8.2.1之前,我从未尝试使用当前项目进行推送通知,因此如果我使用旧的Xcode,我不知道推送通知是否有效。< / p>
答案 0 :(得分:0)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.registerForRemoteNotification(application:application ) // call method to register push notification
return true
}
//MARK : Push Notification
// add this code in AppDelegate.swift outside of classs
import UserNotifications
extension AppDelegate:UNUserNotificationCenterDelegate {
func registerForRemoteNotification(application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert,.sound,.badge], completionHandler: { (complete, error) in
if let err = error {
print(err.localizedDescription)
} else {
UIApplication.shared.registerForRemoteNotifications()
}
})
}
else {
let types:UIUserNotificationType = ([.alert, .sound, .badge])
let settings:UIUserNotificationSettings = UIUserNotificationSettings(types:types, categories:nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert,.sound,.badge])
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print(deviceToken)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Fail to register for notification : \(error.localizedDescription)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if UIApplication.shared.applicationState == .active {
if let dictAps = userInfo["aps"] as? NSDictionary {
if let message = dictAps["alert"] as? String {
print(message) // show
}
}
}
}
}
答案 1 :(得分:0)
导入UserNotifications
框架并在AppDelegate.swift中添加UNUserNotificationCenterDelegate 请求用户权限
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForRemoteNotification()
return true
}
func registerForRemoteNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}
//Getting device token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)
}
//In case of error
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("i am not available in simulator \(error)")
}
//In case if you need to know the permissions granted
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
switch setttings.soundSetting{
case .enabled:
print("enabled sound setting")
case .disabled:
print("setting has been disabled")
case .notSupported:
print("something vital went wrong here")
}
}