我在我的应用程序中使用推送通知服务.当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备的顶部向下滑动时显示的屏幕).但是如果应用程序在前台是委托方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
Run Code Online (Sandbox Code Playgroud)
正在调用,但通知屏幕中不显示通知.
我想在通知屏幕上显示通知,无论应用程序是在后台还是前台.我厌倦了寻找解决方案.任何帮助是极大的赞赏.
显然,ios10现在可以实现这一点:
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
Run Code Online (Sandbox Code Playgroud)
这个答案基本上说明了做这件事所需的工具:
当您的应用程序处于打开状态且位于前台时显示库存iOS通知横幅?
我只是不太了解如何将它们放在一起.
我不知道这有多重要,但我无法保留可选的func,xcode要我将其切换为私有.
我正试图展示徽章,文档提供
static var badge: UNNotificationPresentationOptions { get }
Run Code Online (Sandbox Code Playgroud)
这里很少丢失.
然后我假设我想要排除某个视图控制器获取这些徽章并且我没有使用导航控制器这个代码我发现它会起作用吗?:var窗口:UIWindow?
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
print("Found it!!!")
}
}
}
Run Code Online (Sandbox Code Playgroud) ios uilocalnotification swift swift3 unusernotificationcenter
我想在我的应用程序处于前台时收到本地通知,当我尝试使用以下代码时,它永远不会触发通知,但是当我在后台输入应用程序时,它确实触发了。
这是我尝试过的:
//Schedule a Local Notification
func ScheduleNotification(timeInterval: Double, repeats: Bool, notificationBody:String, title:String){
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: repeats)
let center = UNUserNotificationCenter.current()
let identifier = "UYLLocalNotification"
let content = UNMutableNotificationContent()
content.title = title
content.body = notificationBody
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error {
//Something went wrong
print(error.localizedDescription)
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
if Currentcount < data.count {
self.ScheduleNotification(timeInterval: 5.0, …Run Code Online (Sandbox Code Playgroud) 我编码斯威夫特3,我只是试图发送一个通知,现在没有任何延迟或间隔.但是通知永远不会被触发.这是我的代码..
ViewController代码
import UserNotifications
class HomeViewController: UIViewController{
var isGrantedNotificationAccess:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedNotificationAccess = granted
})
if isGrantedNotificationAccess{
triggerNotification()
}
}
//triggerNotification func goes here
}
Run Code Online (Sandbox Code Playgroud)
triggerNotification函数:
func triggerNotification(){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 1.0,
repeats: false) …Run Code Online (Sandbox Code Playgroud)