我正在尝试让我的应用程序在Xcode 8.0中运行,并且遇到了错误.我知道这个代码在以前版本的swift中运行良好,但我假设在新版本中更改了代码.这是我正在尝试运行的代码:
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.shared().registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是"参数标签"(forTypes:,categories :)'与任何可用的重载都不匹配"
是否有一个不同的命令,我可以尝试使其工作?
Xcode 9(iOS 11)在注册Push(远程)通知时向我显示错误/警告.
这是错误消息
这是代码,我尝试过:
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
Run Code Online (Sandbox Code Playgroud)
错误/警告线:
UIApplication.shared.registerForRemoteNotifications()
怎么解决这个?
显然,ios10现在可以实现这一点:
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
Run Code Online (Sandbox Code Playgroud)
这个答案基本上说明了做这件事所需的工具:
当您的应用程序处于打开状态且位于前台时显示库存iOS通知横幅?
我只是不太了解如何将它们放在一起.
我不知道这有多重要,但我无法保留可选的func,xcode要我将其切换为私有.
我正试图展示徽章,文档提供
static var badge: UNNotificationPresentationOptions { get }
Run Code Online (Sandbox Code Playgroud)
这里很少丢失.
然后我假设我想要排除某个视图控制器获取这些徽章并且我没有使用导航控制器这个代码我发现它会起作用吗?:var窗口:UIWindow?
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
print("Found it!!!")
}
}
}
Run Code Online (Sandbox Code Playgroud) ios uilocalnotification swift swift3 unusernotificationcenter
编辑:所以把应用程序放在后台就行了.
原版的:
所以我一直在尝试向新的UNUserNotificationCenter添加通知,但我似乎没有得到它.
我的视图控制器有一个动作:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { …
Run Code Online (Sandbox Code Playgroud) uilocalnotification swift swift3 ios10 unusernotificationcenter
我有一个iPhone应用程序,可以接收推送通知.目前我可以通过转到iPhone设置/通知来禁用我的应用程序的推送通知.
但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知.
它可以完成,因为我在foursqure看到它iphone应用程序做到了.他们在设置呼叫通知设置中有一个部分,用户可以启用或禁用该应用的不同类型的通知.
我在网上寻找一个合适的解决方案,但仍未找到方法.任何人都可以知道如何做到这一点?
提前致谢 :)
push-notification ios swift usernotifications unusernotificationcenter
在我的应用程序中,我想从我的应用程序本身的设置页面启用/禁用推送通知.任何人都可以建议我从应用程序打开/关闭通知中心的应用程序状态的解决方案?
xcode push-notification ios remote-notifications unusernotificationcenter
iOS文档说UNUserNotificationCenter
'removeAllPendingNotificationRequests()是异步的.
我想要做的是:
打电话removeAllPendingNotificationRequests()
去除我所有预定的通知
安排一堆新通知,其中一些可能会或可能不会具有与之前相同的ID
但是由于文档说该方法是在另一个线程上异步运行(并且没有完成回调参数),我担心有时候,根据线程的变化和时间等因素,第1步仍将是我正在创建第2步中的内容,因此它也会杀死我正在制作的一些新通知.
这种东西手动测试有点棘手,因为它取决于时间.所以我很好奇有人知道这是否是我应该担心的事情......
我有一个Xcode项目,Xcode 8.1(8B62)有两个目标,一个用于付费版本的应用程序,另一个用于免费版本.他们都管理遥控器推送通知.由于我使用新的框架UNUserNotificationCenter.framwork对iOS 10进行了更改,因此第一个目标的一切都很好,而第二个目标的链接错误.它使用旧方法在iOS 9下运行良好.我为两个目标做了什么:
这两个目标的代码相同.
不幸的是,对于第二个目标,我有以下错误:
架构arm64的未定义符号:"_ OBJC_CLASS _ $ _ UNUserNotificationCenter",引自:AppDelegate.o中的Objc-class-ref Ld:未找到架构的符号Clang:错误:链接器命令失败,退出代码为1(使用 - v看看调用)
我已经做过几次干净整洁的构建目标文件夹,重新启动Xcode,重新启动计算机但没有做任何事情.
在不同的论坛中找不到任何东西......
欢迎帮助.先感谢您.
我在我的应用程序中使用本地推送通知.当在iOS 10中为通知添加操作按钮时,它不会出现在通知下方.通知正在显示,但通知底部缺少操作按钮.appdelegate代码如下.
import UIKit
import CoreData
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}
let action = UNNotificationAction(identifier: "markCompleted", title: "Mark as Completed", options: [])
let category = UNNotificationCategory(identifier: "todoList", actions: [action], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
return true
}
func applicationWillResignActive(_ application: UIApplication) …
Run Code Online (Sandbox Code Playgroud) 这个问题是关于UserNotifications
iOS 10中的新框架.
我有一个应用程序,在用户在应用程序中执行某个操作后,每隔半小时安排一次本地通知,从1小时开始.
为了避免使用户的锁定屏幕或通知中心混乱,我只希望一次显示一个通知,因此只有一个通知包含最新的相关信息.我实现这一目标的计划是在出现新通知时随时清除所有已发送的通知.
看来这应该是可行的新willPresent
方法UNUserNotificationCenterDelegate
,但它的行为并不像我期望的那样.
这是一个函数,我打电话设置所有通知,从应用程序中的事件后1小时开始,并在事件发生后23.5小时每半小时安排一次通知,直到最后一个通知:
func updateNotifications() {
for hour in 1...23 {
scheduleNotification(withOffsetInHours: Double(hour))
scheduleNotification(withOffsetInHours: Double(hour) + 0.5)
}
}
Run Code Online (Sandbox Code Playgroud)
这是根据实际进度通知的功能mostRecentEventDate
,这是一个Date
集内其他:
func scheduleNotification(withOffsetInHours: Double) {
// set up a Date for when the notification should fire
let offsetInSeconds = 60 * 60 * withOffsetInHours
let offsetFireDate = mostRecentEventDate.addingTimeInterval(offsetInSeconds)
// set up the content of the notification
let content = UNMutableNotificationContent()
content.categoryIdentifier = "reminder"
content.sound = UNNotificationSound.default()
content.title = …
Run Code Online (Sandbox Code Playgroud)