当 iOS 10 应用程序在后台时,有啥方法可以触发本地通知?

Posted

技术标签:

【中文标题】当 iOS 10 应用程序在后台时,有啥方法可以触发本地通知?【英文标题】:Is there any way to trigger local notifications when iOS 10 app is in the background?当 iOS 10 应用程序在后台时,有什么方法可以触发本地通知? 【发布时间】:2017-02-07 19:22:17 【问题描述】:

当我收到在后台启动我的应用程序的远程推送通知 (VoIP PushKit) 时,我正在尝试触发本地通知。

我在 UNUserNotificationCenter 的待处理通知数组中看到我的本地通知。

[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:
^(NSArray<UNNotificationRequest *> * _Nonnull requests) 

    NSLog(@"Local notifications: %@",requests);
];


"<UNNotificationRequest: 0x17162e500; identifier: 2A364020-3BA2-481B-9255-16EBBF2F4484, content: <UNNotificationContent: 0x1708e8080; title:test, subtitle: (null), body: test2, categoryIdentifier: missed, launchImageName: , peopleIdentifiers: (\n), threadIdentifier: , attachments: (\n), badge: 1, sound: <UNNotificationSound: 0x1704bfe60>, hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: YES, shouldLockDevice: YES, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x171624260; repeats: NO, timeInterval: 5.000000>>"

但它在 5 秒后没有出现。 我使用 UNTimeIntervalNotificationTrigger。

当应用程序在前台时,我已成功调用本地通知。我通过按下按钮调用它来使用相同的触发函数。

ios 应用在后台时有什么方法可以触发本地通知?

【问题讨论】:

【参考方案1】:

是的,它可能,如果它不适合你,那么你的代码一定是做错了什么,但你没有展示它,所以不能对此发表评论。

这是一些工作调试代码,我必须在 voip 推送接收时显示通知:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType)
    
        registerBackgroundTask()
        //present a local notifcation to visually see when we are recieving a VoIP Notification
        if UIApplication.shared.applicationState == UIApplicationState.background 
            let notification  = UNMutableNotificationContent()
            notification.title      = "Voip Push"
            notification.body       = "App in background"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger)
            DispatchQueue.main.async 
                UNUserNotificationCenter.current().add(request)
                 (error) in
                    if error != nil
                    
                        let e = error as? NSError
                        NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)")
                    
                
            
        

        else
        
            let notification  = UNMutableNotificationContent()
            notification.title      = "Voip Push"
            notification.body       = "App in foreground"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger)
            DispatchQueue.main.async 
                UNUserNotificationCenter.current().add(request)
                 (error) in
                    if error != nil
                    
                        let e = error as? NSError
                        NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)")
                    
                
            
        

        NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
    

【讨论】:

以上是关于当 iOS 10 应用程序在后台时,有啥方法可以触发本地通知?的主要内容,如果未能解决你的问题,请参考以下文章

有啥方法可以在 Android Emulator 上测试多点触控吗?

当 iOS 在后台并收到远程通知时发送数据到 Watch

是否可以/允许使用 iOS 作为蓝牙设备和云之间的网关(当应用程序在后台时)

当应用程序在使用 testflight 的设备上运行时,有啥方法可以查看日志?

当后台应用程序扫描蓝牙连接的外围设备时

当应用程序在iOS swift 3中处于后台模式时如何每10秒获取一次纬度和经度