当前位置: 首页 > 知识库问答 >
问题:

在Xcode 8/Swift 3.0中注册推送通知?

鲜于海
2023-03-14

我试图让我的应用程序在Xcode 8.0下运行,但遇到了一个错误。我知道这段代码在以前的swift版本中运行良好,但我假设新版本中的代码有所更改。以下是我试图运行的代码:

let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)     
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.shared().registerForRemoteNotifications()

我得到的错误是“参数标签”(forTypes:,categories:)“不匹配任何可用的重载”

有没有其他命令可以让我试着让它工作?

共有3个答案

吕自明
2023-03-14
import UserNotifications  

接下来,转到目标的项目编辑器,在General选项卡中查找链接的框架和库部分。

单击并选择UserNotifications.framework:

// iOS 12 support
if #available(iOS 12, *) {  
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound, .provisional, .providesAppNotificationSettings, .criticalAlert]){ (granted, error) in }
    application.registerForRemoteNotifications()
}

// iOS 10 support
if #available(iOS 10, *) {  
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
    application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {  
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {  
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {  
    application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}

使用通知委托方法

// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {  
    // Convert token to string
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("APNs device token: \(deviceTokenString)")
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {  
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

用于接收推送通知

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    completionHandler(UIBackgroundFetchResult.noData)
}

设置推送通知是为您的应用启用Xcode 8中的功能。只需转到目标的项目编辑器,然后单击功能选项卡。查找推送通知并将其值切换为ON。

查看下面的链接了解更多通知委托方法

处理本地和远程通知UIApplication ation委托-处理本地和远程通知

https://developer.apple.com/reference/uikit/uiapplicationdelegate

祁鸿晖
2023-03-14
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if #available(iOS 10, *) {

        //Notifications get posted to the function (delegate):  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)"


        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

            guard error == nil else {
                //Display Error.. Handle Error.. etc..
                return
            }

            if granted {
                //Do stuff here..

                //Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                }
            }
            else {
                //Handle user denying permissions..
            }
        }

        //Register for remote notifications.. If permission above is NOT granted, all notifications are delivered silently to AppDelegate.
        application.registerForRemoteNotifications()
    }
    else {
        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }

    return true
}
史钊
2023-03-14

导入UserNotifications框架,并在AppDelegate中添加UnuseNotificationCenterDelegate。敏捷的

请求用户权限

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        return true
}

获取设备令牌

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print(deviceTokenString)
}

万一出错

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

        print("i am not available in simulator \(error)")
}

如果您需要了解授予的权限

UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in

            switch settings.soundSetting{
            case .enabled:

                print("enabled sound setting")

            case .disabled:

                print("setting has been disabled")

            case .notSupported:
                print("something vital went wrong here")
            }
        }
 类似资料:
  • 我正在尝试使用parse.com推送通知服务向我的iOS应用添加推送通知,但我遇到了一些问题,我的一些设备没有收到通知。 当前代码 这似乎在某些设备上起作用(在同事的iPhone5上测试过--起作用了,在我老板的iPhone6上测试过--不起作用) 第43行: 警告2: /users/ds/code/rp-ios/rp/appdelegate.swift:44:25:iOS 8.0中不推荐使用“R

  • 我正在尝试使用Azure通知中心向客户端发送推送通知。我读了这篇文章,它使用标签来识别每个用户。 https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-notify-users/ 它可以完成这项工作,但标记的数量有限。我正在考虑存储和使用中心返

  • 在Parse的推送通知中有一个奇怪的行为。您可以在这里下载一个空白项目和做一些实验https://parse.com/downloads/ios/parse-starter-project/latest 要注册推送通知,我们需要中的这段代码 然后,在中,我们包括: 以下是案例: 案例1。如果你的手机没有一个版本的应用程序,而你运行了代码,它会注册推送通知的设备--一切都运行得很好。 案例2。如果最

  • 编辑:我想我可以保存启动选项,当我准备好在以后设置OneSignal时使用它们。在应用程序的后续启动中,将在中按预期调用。但是当用户最初启用通知时,我将使用保存的,因为该应用程序可能在一段时间内不会再次启动(它确实在后台运行)。

  • null Twilio真的支持VoIP推送吗?如果是,这个设置会有什么问题? 谢谢,古文。 在Viktor的指导下编辑:我现在手动创建Voicegrant。我将key属性的值设置为。下面是赠款的样子: 编辑2:我实际上已经升级到twilio-node 2.11.0,但仍然得到错误。下面是生成JWT之前的访问令牌。

  • 当我需要使用Firebase向特定设备发送通知时,这就是JSON结构。那么,我应该如何修改它以向所有设备或两个选定设备发送相同的通知呢? android中使用Firebase向所有设备发送推送通知的JSON结构是什么?