- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想了解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码以允许此功能,但当我的应用程序在前台时没有显示横幅。
这是我在 AppDelegate.swift
中添加的内容:
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in
if willAllow == true
{
// Do something
}
}
UNUserNotificationCenter.current().delegate = self
// Override point for customization after application launch.
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("GO IN HERE")
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
}
}
我的ViewController.swift
:
override func viewDidLoad()
{
super.viewDidLoad()
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()
content.subtitle = "This is a test"
let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
当我的应用首次启动时,我确实看到了允许我的应用推送通知的权限警告,但我根本没有看到任何通知。
我确实看到了日志输出GO IN HERE,因此调用了willPresent
。
还有什么我想念的吗?
最佳答案
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
if !accepted {
print("Notification access denied")
}
}
}
在 AppDelegate 中添加:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler( [.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
关于ios - 为什么我的本地通知没有在 iOS 10 的前台触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221455/
我正在关注 Android guide在媒体播放器应用程序中使用 MediaBrowserServiceCompat,但该服务在应用程序退出时被销毁。
我使用System.Windows.Window.IsActive来检测窗口是否位于前台,并且在某些情况下有效。但我发现了一些情况,它没有,我想知道是否有任何方法可以检测到它。 最佳答案 除非仅后台进
我需要为 EditText 的一部分设置样式。我希望文本为白色,背景为灰色。看起来很简单,但事实并非如此。 spanRange.setSpan(new BackgroundColorSpan(Colo
我决定在一个帖子中发布两个问题,因为这是完全相同的问题。 我需要知道屏幕何时打开或关闭,以便我可以打开 LED。第二个我需要知道我的应用程序是在后台还是在前台,以便在应用程序处于后台时管理在某些操作上
关于双高红色录音状态栏有很多问题( here , here ),但是当应用程序退出到后台时,所有这些问题都引用闪烁。我得到了一个闪光,我假设来自 AVCaptureSession设置,而应用程序在前台
我有一个奇怪的案例。我的 swift ios 应用程序已连接到 Cloudkit。如果应用程序未运行(后台状态),我每次都会收到通知徽章和警报!如果应用程序正在运行,则不会收到任何通知!我知道它没有击
我是 firebase 和 android 的新手,我想在我的应用程序中包含实时聊天。但我对 firebase 有以下疑问。请帮忙。 1) 如果应用程序在前台,系统托盘中是否会有默认通知,还是我必须在
我正在为 Excel 编写 VSTO 加载项,我注意到,如果我锁定工作表并使用密码保护它(因此只有我的加载项可以写入它,但用户可以查看它),如果用户尝试编辑工作表时,他们收到“此工作表已锁定”弹出窗口
我正在开发一个 iOS 应用程序,此应用程序允许其用户添加他稍后必须执行的任务。完成添加此任务后,它将发送到服务器以保存在服务器端。现在我对某些情况感到困惑:我的用户在输入任务详细信息时有什么电话..
我正在阅读内存不足 (OOM) killer ,以及 Android 如何确定进程的优先级 (https://developer.android.com/guide/components/proces
我已经在我的新应用程序中启动了一项服务。该服务是前台的,带有通知。当它在 AVD 2.1 API Level 7 中运行时,一切正常。但是当它在运行 Gingerbread 的 Samsung Gal
我的 Laravel 应用程序的结构需要帮助。 我想要的基本上是这个结构: 应用程序接口(interface) 管理面板 公共(public)网站 我开始构建我认为非常正确的文件夹结构: app/
我正在尝试用 CardView 填充我的 RecyclerView,CardView 使用 Android 数据绑定(bind)来设置属性,例如 TextView 中的文本。在未完成喷射的项目上,我想
我想使用 Window Script Host(WSH) 查找当前事件(具有焦点)的窗口的标题,因为我希望我的 WSH 脚本仅在所需窗口处于事件状态时才发送键。 注意*我无法使用替代方案,即在调用 s
如何调试 react-scripts 启动? 这工作正常,我不知道发生了什么变化(我没有改变任何东西) 看来 react-scripts start 无法作为前台进程继续运行。 我的 Dockerfi
我想制作像endonmondo那样的秒表。当我们启动应用程序时,它应该计算时间并更新主 Activity 中的textView(实际上它是一个 fragment )。 我做了后台服务(如 here )
我遇到了一个需求,但我无法获得正确的实现方式,因此需要您的帮助。 我想做什么? - 我想根据收到的通知执行操作,如下所示: 当应用程序打开并位于前台时,即对用户可见并且我收到通知时,我只是显示一个弹出
在 Android 10 中,我注意到我从操作系统收到一条 Toast 消息,说明“此 NFC 标签不支持应用程序”或“此 NFC 标签不支持应用程序”(取决于设备): 奇怪的是,我在 enableR
我是一名优秀的程序员,十分优秀!