当iOS 10应用程序在后台时,有没有办法触发本地通知?

时间:2017-02-07 19:22:18

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

我在接收远程推送通知(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 个答案:

答案 0 :(得分:0)

是的,它是可能的,如果它不适合你,那么你必须对你的代码做一些不正确的事情,但是你没有显示它,所以不能对此发表评论。

这是一些有效的调试代码,我必须在voip push接收时显示通知:

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)")
    }