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

2024-05-09

我在我的应用程序中使用本地推送通知。在 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) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  }

  func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  }

  func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  }

  func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  }

  func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()
  }


  // MARK: - user defined functions

  func notifyWithinApp(_ message: String) {
    Utils.sharedInstance.notifyLocally(message)
  }

  func scheduleNotification(at date: Date, body: String) {
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

    let content = UNMutableNotificationContent()
    content.title = "Dont Forget"
    content.body = body
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "todoList"

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
      if let error = error {
        print("Uh oh! We had an error: \(error)")
      }
    }
  }


  // MARK: - Core Data stack

  lazy var persistentContainer: NSPersistentContainer = {
      /*
       The persistent container for the application. This implementation
       creates and returns a container, having loaded the store for the
       application to it. This property is optional since there are legitimate
       error conditions that could cause the creation of the store to fail.
      */
      let container = NSPersistentContainer(name: "ListTodo")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
          if let error = error as NSError? {
              // Replace this implementation with code to handle the error appropriately.
              // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

              /*
               Typical reasons for an error here include:
               * The parent directory does not exist, cannot be created, or disallows writing.
               * The persistent store is not accessible, due to permissions or data protection when the device is locked.
               * The device is out of space.
               * The store could not be migrated to the current model version.
               Check the error message to determine what the actual problem was.
               */
              fatalError("Unresolved error \(error), \(error.userInfo)")
          }
      })
      return container
  }()

  // MARK: - Core Data Saving support

  func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
          do {
              try context.save()
          } catch {
              // Replace this implementation with code to handle the error appropriately.
              // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
              let nserror = error as NSError
              fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
          }
      }
  }

}

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

  // Receive displayed notifications for iOS 10 devices.
  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              willPresent notification: UNNotification,
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Print message ID.
    // Print full message.
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    if response.actionIdentifier == "markCompleted" {
    }
  }
}

这些按钮不会单独出现。在支持的设备上,您必须通过 3D 触摸通知来显示内容或按钮。在不受支持的设备上,您可以尝试向下或向左/向右滑动以显示按钮。

希望这可以帮助。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

操作按钮未出现在通知 iOS 10 中 的相关文章

  • Swift 客户端和根 SSL 证书身份验证

    积分得到token api of QLIK server with ssl证书 但执行任务后我收到这样的错误 完成时出现错误 代码 999 Your hostname and endpoint let hostname YOUR HOST
  • 数组索引超出范围的表视图

    我正在使用数组从数据库读取数据 目前数组中有 8 个项目 我正在尝试制作一个有节标题的表格 目前我有 4 个部分 并且我已正确设置并且它有效 它也可以在第一次运行时运行 但是当我尝试向后滚动时 我发现索引超出了范围 我正在使用 myarra
  • 有没有办法以编程方式选择 Segue 锚点?

    假设我有一个 Storyboard 其中包含一个包含按钮的视图 当用户按下此按钮时 会出现一个弹出窗口 因此 我需要使用 Xcode 将 Segue 拖动到按钮来设置锚点 然后执行使用标识符执行Segue 所以 我的问题是 有没有办法以编程
  • 我可以在滚动时固定表格的 tableHeaderView 位置吗?

    我有一个表视图 并在其 tableHeaderView 上附加了一个 UISegmentedControl 如何使 tableHeaderView 固定 以便即使在滚动表视图时也始终可以在同一位置查看 UISegmentedControl
  • 排除 Realm 模型类

    我的应用程序中配置了两个领域文件 我想存储我的Log将模型与其他模型分开保存为单独的文件 我的问题是我也看到了我的Log我不想要的默认 Realm 文件中的模型类 如何从给定的 Realm 文件中排除特定的模型类 我使用主 Realm 文件
  • Modal UIViewController 在 iPad 上总是全屏显示。为什么?

    我试图在 iPad 上创建一个简单的模式对话框 无论是小设置 UIModalPresentationFormSheet 还是大设置 UIModalPresentationPageSheet 但无论我做什么 它们都会全屏显示 带有标题栏 模态
  • Swift 中使用 T 型进行泛型调用

    在我的应用程序中 我想创建一个通用方法 该方法根据给定类型 T 创建一个对象数组 我创建了以下函数 func getArray
  • 我什么时候应该使用 CGM 设备组和主题?

    我正在考虑将 Google Cloud Messaging 合并到我的应用程序中 我想向与用户关联的所有设备发送消息 乍一看 它看起来像 GCM 的设备群组消息传递 https developers google com cloud mes
  • 如何更改某些功能以兼容 iOS 10 或更低版本的 Snapchat 中的某些功能,例如相机视图控制器

    我正在制作一个视图控制器来制作像 snapchat 相机一样的相机视图控制器 我下面的代码在 iOS 11 或更高版本上完美运行 老实说 我并没有真正掌握我的代码 因为我只是按照这个像相机视图控制器这样的 snapchat 的教程进行操作
  • 从钥匙串保存和加载 |斯威夫特[重复]

    这个问题在这里已经有答案了 如何简单地将字符串存储在钥匙串中并在需要时加载 有几种SO解决方案 主要参考Git repo 但我需要最新 Swift 上最小和最简单的解决方案 当然 我不想添加 git 框架来简单地在我的项目中存储密码 有类似
  • NSNotification 与dispatch_get_main_queue

    和 关联这个问题 https stackoverflow com questions 7905192 iphone grand central dispatch main thread我想知道关于何时使用 NSNotification 在主
  • 将 NSDictionary 保存到文件有任何限制吗

    我想用下面的方法来保存NSDictionary void writeDicToFile NSDictionary dic fileName NSString fileName NSString filePath NSTemporaryDir
  • iOS TestFlight - 外部测试人员未收到新版本通知

    我已经向外部 TestFlight 用户提供了一个应用程序 他们已收到电子邮件并按预期安装了应用程序 他们有版本 1 0 Build 1 不过 我现在已经上传了一个新版本 版本 1 0 版本 2 这已获得批准 在外部测试页面中 我添加并选择
  • 如何在调用-reloadData后保留UITableView contentoffset

    CGPoint offset table contentOffset table reloadData table setContentOffset offset animated NO unuseful block UITableView
  • 如何在 Cocoa with Core Data 中上传和存储文件?

    我有一个应用程序 我希望能够将文件上传到其中 我可以想象序列化数据 将其放入数组中 然后序列化数组并将其放入核心数据中 但这似乎不太正确 我还可以想象将文件复制到应用程序的支持文件中 获取这些文件的 NSURL 并存储数组或 NSURL 无
  • 快速从 appDelegate 中 popToRootViewController

    我试图从应用程序委托弹出到导航堆栈的根视图控制器 并且在将 obj c 中的内容转换为 swift 时遇到一些问题 obj c 中的工作原理 UINavigationController navigationController UINav
  • 启动时运行后台任务

    我正在编写一个 iOS 应用程序 它使用 Dropbox Datastore API 在多个设备之间同步数据 在 Android 上 我可以告诉服务在启动时启动 这使其能够同步 设备关闭时可能发生的任何更改 我无法找到让我的应用程序在 iO
  • 播放声音无延迟 iOS

    我找不到如何以低延迟播放真实声音的方法 我尝试使用 AVFoundation 音频播放器 巨大的延迟大约 500 毫秒 所以我尝试创建系统声音 如果运气不好 延迟大约为 200 毫秒 虽然不多 但对我来说没有用 我最多需要 50 毫秒 确保
  • 奇怪的 Facebook ID [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我有两个 Facebook 应用程序 它们都正在开发中 其中一个是很久以前创建的 而另一个则是相当新的 自从我升级到新的 iOS Faceb
  • 无法在 Swift 中获取 plist URL

    我对这个真的很困惑 网络上有很多问题询问 如何从 Swift 中的 plist 文件获取信息 并且到处都发布了相同的答案 let path NSBundle mainBundle pathForResource Config ofType

随机推荐

  • 在 web.xml 中引用环境变量

    我正在预打包一个 JSP Web 应用程序 该应用程序依赖于 web xml 中的某些文件路径设置 这些设置在打包时是未知的 因为它们引用了客户在部署整个应用程序 其中 Web 应用程序是管理界面 时将设置的路径 似乎避免安装程序脚本中的令
  • 在 docker-compose 构建之后,docker-compose up 运行旧的未更新的容器

    我使用 docker compose 并发现以下问题 当我更改代码并想要重建我使用的泊坞窗时 docker compose stop docker compose build 然后我想通过以下方式运行系统 docker compose up
  • 模拟后 CreateMutex 失败

    这是我尝试模拟用户然后创建互斥体的代码 未创建互斥体 我收到 ERROR ACCESS DENIED 错误 void Impersonate DWORD logonType LOGON32 LOGON INTERACTIVE DWORD l
  • 编写/转换 Meteor 同步函数

    这已经困扰我一段时间了 所以我想我应该对其进行快速的质量检查 如果有一个普通的nodeJS模块或其他东西 并且它在服务器端有一个异步功能 我如何使其同步 例如我如何转换nodejsfs stat异步函数转为同步函数 例如我有 服务器端js
  • 使用 angular-cli 运行 Protractor 时 SpecReporter 不是构造函数错误

    当我尝试跑步时ng e2e或直接protractor命令我收到此错误 14 39 00 I update chromedriver chromedriver 2 28 up to date 14 39 04 I launcher Runni
  • 为什么 Django 的 URLField 默认截断为 200 个字符?

    我喜欢 Django 并且经常使用它 我发现它的大部分默认设置都是正常的 但有一个一直困扰着我 以至于我在每个项目中都覆盖了它 a 的默认最大长度URLField https github com django django blob st
  • 老虎普查数据中的 Shape_area 和 ALAND AWATER

    Shape area 是什么意思 我注意到它不是 Aland 和 Awater 的总和 这三个的单位是什么 ALAND和AWATER的单位是平方米 例如 tl 2015 us county dbf康明县有记录ALAND 1477895811
  • RestEasy @Path 正则表达式问题

    是否可以使用可选路径变量定义路径 就像下面的 uri app make make model model year year mileage mileage fuelType fuelType maxPrice maxprice trans
  • 如何通过情节提要向导航栏添加后退按钮?

    我现在很困惑 我在网上到处都看到添加自定义后退按钮的教程 但我什至似乎无法启动默认按钮 在我的 MainViewController 中 我有performSegueWithIdentifier 然后在另一端 我希望导航栏在左侧有后退按钮
  • 我应该开发一个单独的 Express 服务器,还是处理 next.js 应用程序中的所有 API 调用?

    我的网站将执行 CRUD 操作 并将与 MongoDB 和 Firebase 存储 身份验证配合使用 开发单独的 Express 服务器而不是将所有内容集成到我的 next js 应用程序中的原因 优势是什么 据我所知 这一切都可以在我的
  • Cloudwatch 仪表板:具有独立时间范围的小部件?

    我希望仪表板中的每个小部件都有自己的时间范围 我找不到办法做到这一点 如果不可能的话 我会发现它令人难以置信 我希望我的仪表板包含以下内容堆积区域小部件 过去一小时内 30 秒周期的请求计数 即 120 个绘图点 过去一天以 1 小时为周期
  • 当应用程序停止使用 Sencha Touch 2.2 和 Phonegap 时,LocalStorage 存储不会保留在 Android 手机上

    这在我的浏览器中工作正常 但是当我在手机上安装该应用程序并使用它时 它看起来很好 直到我强制它停止并重新打开该应用程序 然后我的所有记录都消失了 我正在使用 2 2 和 Phonegap 任何帮助将非常感激 这是我的商店 Ext defin
  • 无法在Python中打开html5lib

    我刚刚使用 Windows 命令提示符安装了适用于 Python 的 html5lib 该软件包安装在这里 File C Python27 lib site packages html5lib 但是 如果我尝试导入 html5lib usr
  • gradle 过滤 src/main/webapp 中文件子集的方法是什么?

    我正在将 Maven 转换为 gradle 希望了解执行以下操作的最佳方式的意见 我目前在 src main webapp 下有多个文件 有些需要以一种方式过滤 有些需要以另一种方式过滤 理论上 在 src main webapp 下 我有
  • 在 Angular 中,如何动态地将某些单词包装在另一个 html 元素中?

    我有这个简单的角度组件 Component selector my component template p someString p export class MyComponent Input someString string som
  • Perl 6:检查元素是否在列表中的最佳方法是什么?

    假设我有一个大数组 stuff and a thing 我想知道是否 thing is in stuff 在 Perl 6 中做到这一点的最佳方法是什么 我所说的 最好 是指 惯用的 可读的 高性能的 不一定按这个顺序 实际上有两个单独的案
  • 重用 Jsoup 连接

    我喜欢 Jsoup 来解析 html 但它们的连接有问题 我需要向同一个网站但不同的查询参数发送请求 比如 id XXX 请求如下 http website id XXX 我不想为每个 id 创建一个新连接 而是为所有 id 请求保留一个连
  • 将 DIV 扩展到父列表项之外

    我有一个有序列表 它实际上是页面上产品的显示 在每个列表项中 li 有一些内容 后面跟着一个div在关闭列表项之前包含更多内容 我需要每个列表项中的 div 扩展 其宽度 超出其父列表项 并实际填充有序列表的宽度 ol 每个 div 还需要
  • iOS 中的 FacebookSDK 不显示关闭按钮且无法关闭

    您好 我已经为 iOS 6 应用程序集成了 Facebook SDK Facebook 身份验证和共享工作完美 但没有提供关闭 FB 对话框的规定 当FB对话框打开时 只有在身份验证成功后才会关闭 没有关闭或导航回来的规定 我如何制作关闭按
  • 操作按钮未出现在通知 iOS 10 中

    我在我的应用程序中使用本地推送通知 在 iOS 10 中为通知添加操作按钮时 它不会出现在通知下方 通知正在显示 但通知底部缺少操作按钮 下面给出了 appdelegate 代码 import UIKit import CoreData i