Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 应用程序处于后台时不显示UNUserNotification_Ios_Swift_Unusernotificationcenter - Fatal编程技术网

Ios 应用程序处于后台时不显示UNUserNotification

Ios 应用程序处于后台时不显示UNUserNotification,ios,swift,unusernotificationcenter,Ios,Swift,Unusernotificationcenter,我正试图在指定的延迟后在后台获取本地通知,但由于某些原因,在我的应用程序中进行计划时,不会显示该通知。我创建了一个裸体测试应用程序来测试相同代码在指定延迟后按预期显示计划通知的行为,因此我已确认我的代码是正确的。但是,在我的应用程序中,相同的代码不会产生相同的结果。我有正确的权限等,但通知没有显示。它将显示在前台,而不是应用程序在后台时。我还检查了调度后挂起的通知,查看它是否在队列中,并且确实在队列中。我在代码中搜索了对UNUserNotificationCenter.current()的任何引

我正试图在指定的延迟后在后台获取本地通知,但由于某些原因,在我的应用程序中进行计划时,不会显示该通知。我创建了一个裸体测试应用程序来测试相同代码在指定延迟后按预期显示计划通知的行为,因此我已确认我的代码是正确的。但是,在我的应用程序中,相同的代码不会产生相同的结果。我有正确的权限等,但通知没有显示。它将显示在前台,而不是应用程序在后台时。我还检查了调度后挂起的通知,查看它是否在队列中,并且确实在队列中。我在代码中搜索了对
UNUserNotificationCenter.current()
的任何引用,并添加了断点和注释代码,以确保没有其他内容与之交互

以下是触发通知并验证其已添加到队列的代码:

let content=UNMutableNotificationContent()
content.title=“点击触发测试”
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:5,重复:false)
let request=UNNotificationRequest(标识符:UUID().uuidString,内容:content,触发器:trigger)
UNUserNotificationCenter.current()。在中添加(请求){(错误)
打印(“错误”,错误)
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler:{(请求)在中)
打印(“请求”,请求.计数)
})
}
下面是my
AppDelegate
中的权限注册和
UnuseNotificationCenterDelegate
实现:

public func userNotificationCenter(center:unuservificationcenter,将显示通知:UNNotification,withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){
completionHandler(.alert)
}
public func userNotificationCenter(center:UNUserNotificationCenter,didReceive响应:UNNotificationResponse,withCompletionHandler completionHandler:@escaping()->Void){
打印(答复)
}
公共函数注册表形式通知(){
UNUserNotificationCenter.current().requestAuthorization(选项:[.alert、.sound、.badge]){[weak self]已授予,错误在
打印(“已授予权限:\(已授予)”)
授予else{return}
self?.getNotificationSettings()
}
}
公共函数getNotificationSettings(){
UnuseNotificationCenter.current().getNotificationSettings{中的设置
打印(“通知设置:\(设置)”)
guard settings.authorizationStatus==.authorized else{return}
DispatchQueue.main.async{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
控制台输出:

Permission granted: true
Notification settings: <UNNotificationSettings: 0x280565260; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, carPlaySetting: NotSupported, criticalAlertSetting: NotSupported, alertSetting: Enabled, alertStyle: Banner, providesAppNotificationSettings: No>
error nil 
requests 1
授予的权限:true
通知设置:
零误差
请求1

我还应该补充一点,在测试中,有几次它是随机发射的。有一次,当我在添加请求完成块中设置断点时…

尝试向内容添加正文:

content.body = "Some content"

我不知道其他示例应用程序是如何工作的,而您的主应用程序不使用相同的代码。但我也不明白没有内容时前台通知是如何显示的。body

尝试在内容中添加body:

content.body = "Some content"

我不知道其他示例应用程序是如何工作的,而您的主应用程序不使用相同的代码。但我也不明白在没有内容的情况下前台通知是如何显示的。body

您在控制台中得到了什么跟踪?@barbarity请参阅updatery以删除所有挂起的通知,然后重试或重新启动设备。根据代码,它应该是完美的,但看起来你的设备工作不正常。@barbarty我两次都尝试了,但都没有乐趣。我不相信这是设备,但因为测试应用程序运行良好。。。困惑:(不清楚什么“有效”和“无效”意思是。你期望什么?实际发生了什么?你在控制台中得到了什么跟踪?@barbarity请参阅Updatery以删除所有挂起的通知,然后重试或重新启动你的设备。根据代码,它应该是完美的,但看起来你的设备工作不正常。@barbarity我两次都尝试了,但都不高兴。不过,我不相信它就是这个设备由于测试应用程序运行良好…困惑:(不清楚“有效”和“无效”是什么意思。你期望什么以及实际发生了什么?@droovers你试过我的答案了吗?@droovers你试过我的答案了吗?