操作按钮未出现在通知 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 中 的相关文章

随机推荐

  • 在 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