【问题标题】: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 应用在后台时有什么方法可以触发本地通知?

【问题讨论】:

    标签: ios background push-notification apple-push-notifications nsusernotificationcenter


    【解决方案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)")
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2018-01-07
      相关资源
      最近更新 更多