标签: unusernotificationcenter

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

我正在尝试让我的应用程序在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 :)'与任何可用的重载都不匹配"

是否有一个不同的命令,我可以尝试使其工作?

push-notification ios swift3 ios10 unusernotificationcenter

116
推荐指数
7
解决办法
10万
查看次数

必须仅从主线程调用UIApplication.registerForRemoteNotifications()

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()

怎么解决这个?

push-notification ios swift unusernotificationcenter swift4

71
推荐指数
5
解决办法
3万
查看次数

当应用程序在前景Swift 3时获取本地通知

显然,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

35
推荐指数
4
解决办法
4万
查看次数

在ios10中添加本地通知 - swift 3

编辑:所以把应用程序放在后台就行了.

原版的:

所以我一直在尝试向新的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

27
推荐指数
2
解决办法
5万
查看次数

在应用内启用或禁用Iphone推送通知

我有一个iPhone应用程序,可以接收推送通知.目前我可以通过转到iPhone设置/通知来禁用我的应用程序的推送通知.

但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知.

它可以完成,因为我在foursqure看到它iphone应用程序做到了.他们在设置呼叫通知设置中有一个部分,用户可以启用或禁用该应用的不同类型的通知.

我在网上寻找一个合适的解决方案,但仍未找到方法.任何人都可以知道如何做到这一点?

提前致谢 :)

push-notification ios swift usernotifications unusernotificationcenter

26
推荐指数
2
解决办法
3万
查看次数

如何启用/禁用应用程序的推送通知?

在我的应用程序中,我想从我的应用程序本身的设置页面启用/禁用推送通知.任何人都可以建议我从应用程序打开/关闭通知中心的应用程序状态的解决方案?

xcode push-notification ios remote-notifications unusernotificationcenter

25
推荐指数
2
解决办法
3万
查看次数

我怎么知道UNUserNotificationCenter的removeAllPendingNotificationRequests()何时完成?

iOS文档说UNUserNotificationCenter'removeAllPendingNotificationRequests()是异步的.

我想要做的是:

  1. 打电话removeAllPendingNotificationRequests()去除我所有预定的通知

  2. 安排一堆新通知,其中一些可能会或可能不会具有与之前相同的ID

但是由于文档说该方法是在另一个线程上异步运行(并且没有完成回调参数),我担心有时候,根据线程的变化和时间等因素,第1步仍将是我正在创建第2步中的内容,因此它也会杀死我正在制作的一些新通知.

这种东西手动测试有点棘手,因为它取决于时间.所以我很好奇有人知道这是否是我应该担心的事情......

cocoa-touch ios unusernotificationcenter

15
推荐指数
1
解决办法
328
查看次数

UNUserNotificationCenter链接错误

我有一个Xcode项目,Xcode 8.1(8B62)有两个目标,一个用于付费版本的应用程序,另一个用于免费版本.他们都管理遥控器推送通知.由于我使用新的框架UNUserNotificationCenter.framwork对iOS 10进行了更改,因此第一个目标的一切都很好,而第二个目标的链接错误.它使用旧方法在iOS 9下运行良好.我为两个目标做了什么:

  • 功能:启用推送通知(权利已正确更新)
  • 在AppDelegate.h中:#import
  • 在AppDelegate.m中:使用新方法更新了代码

这两个目标的代码相同.

不幸的是,对于第二个目标,我有以下错误:

架构arm64的未定义符号:"_ OBJC_CLASS _ $ _ UNUserNotificationCenter",引自:AppDelegate.o中的Objc-class-ref Ld:未找到架构的符号Clang:错误:链接器命令失败,退出代码为1(使用 - v看看调用)

我已经做过几次干净整洁的构建目标文件夹,重新启动Xcode,重新启动计算机但没有做任何事情.

在不同的论坛中找不到任何东西......

欢迎帮助.先感谢您.

xcode ios ios10 unusernotificationcenter

14
推荐指数
1
解决办法
7357
查看次数

操作按钮未出现在通知iOS 10中

我在我的应用程序中使用本地推送通知.当在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)

ios swift swift3 unusernotificationcenter

12
推荐指数
2
解决办法
3174
查看次数

在iOS 10中使用UNUserNotificationCenterDelegate收到新通知时,如何删除以前发送的通知?

这个问题是关于UserNotificationsiOS 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)

notifications ios ios10 unusernotificationcenter

11
推荐指数
1
解决办法
4651
查看次数