Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 本地通知已计划,但未以挂起和未运行模式传递_Ios_Swift_Apple Push Notifications_Background Fetch - Fatal编程技术网

Ios 本地通知已计划,但未以挂起和未运行模式传递

Ios 本地通知已计划,但未以挂起和未运行模式传递,ios,swift,apple-push-notifications,background-fetch,Ios,Swift,Apple Push Notifications,Background Fetch,我想在静默远程通知到达时为我的应用程序添加后台抓取(内容可用:1),因此当它到达时,我在本地触发后台更新,检查新数据,如果是,则为用户生成本地推送通知。但是,它只有在应用程序处于后台、未挂起或未运行状态时才能完美工作。在这些状态下,我让它获取一些数据,但不会触发本地通知(它是在设备控制台中创建的),有什么办法可以解决这个问题吗 这是我生成推送的代码(它按预期工作) func generatePushNotification(标题:String,正文:String,徽章:Int=1){ let c

我想在静默远程通知到达时为我的应用程序添加后台抓取(内容可用:1),因此当它到达时,我在本地触发后台更新,检查新数据,如果是,则为用户生成本地推送通知。但是,它只有在应用程序处于后台、未挂起或未运行状态时才能完美工作。在这些状态下,我让它获取一些数据,但不会触发本地通知(它是在设备控制台中创建的),有什么办法可以解决这个问题吗

这是我生成推送的代码(它按预期工作)

func generatePushNotification(标题:String,正文:String,徽章:Int=1){
let content=UNMutableNotificationContent()
content.badge=作为NSNumber的徽章
content.title=标题
content.body=body
content.sound=UNNotificationSound.default
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:1,
重复:错误)
let notification=UNNotificationRequest(标识符:“pushLocal”,内容:content,触发器:trigger)
UNUserNotificationCenter.current()。在中添加(通知){(错误)
如果错误!=nil{
NSLog((错误?.localizedDescription)!)
}
}
}
这是我的notificationFetch代码

func应用程序(application:UIApplication,DidReceiveMemoteNotification用户信息:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)->Void){
networkManager.getScheduleForGroup(组){(响应,错误)位于
self.generatePushNotification(带“Back”,正文:“ground”)
completionHandler(.newData)
}
completionHandler(.noData)
}
以下是控制台输出:

dasd    CANCELED: com.apple.pushLaunch.**:297454 <private>!

SpringBoard    Saving notification 802A-AE8C: 0 [ hasAlertContent: 0, shouldPresentAlert: 1 ]

SpringBoard  Delivered user visible push notification 802A-AE8C

SpringBoard  Load 0 pending notification dictionaries

SpringBoard  Adding notification 802A-AE8C to destinations 0 [ hasAlertContent: 0, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]
dasd取消:com.apple.pushLaunch.*:297454!
跳板保存通知802A-AE8C:0[hasAlertContent:0,shouldPresentAlert:1]
SpringBoard交付的用户可视推送通知802A-AE8C
SpringBoard加载0个挂起的通知字典
SpringBoard向目的地0添加通知802A-AE8C[hasAlertContent:0,shouldPresentAlert:1 hasSound:0 shouldPlaySound:1]
使用此

//AppDelegate

UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })


application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
//在视图控制器中

 let content = UNMutableNotificationContent()
        content.title = “Notification Title”
        content.body = “Notification Body”
        content.sound = UNNotificationSound.default()
        content.badge = 1

        let date = Date()

        let localDate = date.toLocalTime()

        var triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: localDate)
        triggerDaily.hour = 22
        triggerDaily.minute = 00
        triggerDaily.second = 00

        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)

        let identifier = "Local Notification"
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request) { (error) in
            if let error = error {
                print("Error \(error.localizedDescription)")
            }
        }
//添加转换时间的扩展

extension Date { 

  func convertToGlobalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = -TimeInterval(currentTimezone.secondsFromGMT(for: self))
     return Date(timeInterval: interval, since: self)
 }

 func convertToLocalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = TimeInterval(currentTimezone(for: self))
     return Date(timeInterval: interval, since: self)
 }

}

静默远程通知并不意味着推送通知,而不是本地通知。@AnkitJayaswal你是什么意思?静默通知适用于具有
内容可用
键的推送通知,但不适用于本地通知。关于更多细节-@AnkitJayaswal我不想要一个无声的本地通知,而是希望它带有声音。静默是我的远程通知。您不应该在闭包之外调用
completionHandler(.noData)
。这向ios表明您没有数据,并将导致您的应用程序在网络操作完成之前再次挂起。这无助于notification=UILocalNotification()notification.fireDate=NSDate(timeIntervalSinceNow:1)作为日期通知。alertBody=body notification.alertAction=title notification.soundName=UILocalNotificationDefaultSoundName UIApplication.shared.scheduleLocalNotification(通知)使用此
extension Date { 

  func convertToGlobalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = -TimeInterval(currentTimezone.secondsFromGMT(for: self))
     return Date(timeInterval: interval, since: self)
 }

 func convertToLocalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = TimeInterval(currentTimezone(for: self))
     return Date(timeInterval: interval, since: self)
 }

}