- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
为了让推送通知在 iOS10 中工作,我把头发扯下来。当前设置:
在 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
:
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if error == nil {
print("DID REQUEST THE NOTIFICATION")
UIApplication.shared.registerForRemoteNotifications()
}
}
print("DID SET DELEGATE")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
:
print("DID REGISTER FOR A REMOTE NOTIFICATION AND THE TOKEN IS \(deviceToken.base64EncodedString())"
let request = UpdatePushNotificationSubscription_Request(deviceToken: deviceToken)
updatePushNotificationSubscriptionWorker.updateSubscription(request)
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("GOT A NOTIFICATION")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//This is for the user tapping on the notification
print("GOT A NOTIFICATION")
}
最佳答案
此答案适用于 iOS 10+,使用 UserNotifications
框架。
您需要一个符合 UNUserNotificationCenterDelegate
的类协议(protocol)。如果您为此创建一个新类,或者将其添加到您的 AppDelegate
中,则无关紧要。类(class)。不过,我建议创建一个专门的类(class)。出于此答案的目的,假设您创建了 UserNotificationController
为它上课。
该类可以具有以下方法:optional func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
optional func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
然后在你的AppDelegate.application(_:didFinishLaunchingWithOptions:)
方法,你需要设置delegate
在 UNUserNotificationCenter.current()
反对您的 UserNotificationController
的实例类(class)。您可能想要使用共享实例。
使用 UNUserNotificationCenter.requestAuthorization(options:completionHandler:)
请求用户授权以启用通知方法,并在 completionHandler
,查看granted
值(value)。如果 true
, 调用UIApplication.shared.registerForRemoteNotifications()
注册远程通知.
现在,当应用程序收到推送通知时,可能会发生几种不同的情况。我将尝试在此处列出最常见的情况。
本地通知:
如果应用程序在前台,应用程序将调用 UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:)
.
如果应用程序在后台(运行与否),则在用户点击通知之前不会调用任何内容,此时,应用程序将打开并调用 UserNotificationController .userNotificationCenter(_:didReceive:withCompletionHandler:)
.
远程通知:
有效载荷的内容将影响发生的事情。有效载荷有三种情况,a) 只是正常的 alert
, badge
, 和 sound
选项 b) 包括 content-available
选项(设置为 1
或 true
) c) 包括 mutable-content
选项(设置为 1
或 true
)。另外,从技术上讲,d) 你同时拥有 content-available
和 mutable-content
,但这只会触发这两种情况。
对于 a) 只需 alert
, sound
, badge
信息:
这与本地通知的工作方式相同。
对于 b) content-available
== 真:
如果应用程序在前台,UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:)
叫做。
如果应用程序在后台(运行与否),AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
被调用,而不是您的 UserNotificationController
中的方法之一类(class)。
对于 c) mutable-content
== 真:
如果您添加了 UNNotificationServiceExtension
对于您的应用程序,它将处理通知并可以修改内容。无论您的主应用程序的状态如何,都会发生这种情况。如果用户点击了(可选修改的)通知,则会像上面的本地通知一样对其进行处理。
没有 UNNotificationServiceExtension
,通知被视为上面的普通远程通知。
附加说明:
使用 mutable-content
时,您必须包含 alert
有效载荷中的信息,否则系统会将其视为不可变的并且不会调用您的UNNotificationServiceExtension
.您修改后的通知仍必须包含 alert
info,否则将使用原始通知有效负载。遗憾的是,没有办法阻止通知出现在用户面前。
使用 content-available
时,如果用户在上次使用时强行退出应用,系统不会重新启动应用或调用AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
.尽管它仍会显示任何警报、播放声音并按照有效负载中的指示更新角标(Badge)。
关于未调用 iOS 10 UNUserNotificationCenterDelegate。推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39826905/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!