【问题标题】:iOS 10 How to view a list of pending notifications using UNUserNotificationCenter?iOS 10 如何使用 UNUserNotificationCenter 查看待处理通知列表?
【发布时间】:2017-03-09 07:27:38
【问题描述】:

解决方案代码:

let center = UNUserNotificationCenter.current()
print(center.getPendingNotificationRequests(completionHandler: { error in
        // error handling here
    }))

我的原帖:

我正在尝试通过 UNUserNotificationCenter 获取待处理通知列表,因为 UIApplication.shared.scheduledLocalNotifications 已被贬值。

这是我正在使用的代码:

let center = UNUserNotificationCenter.current()
    print(UNUserNotificationCenter.getPendingNotificationRequests(center))

但是,这会打印“(功能)”。 getPendingNotificationRequests 需要一个 UNUserNotificationCenter 参数,我想不出它还能是什么。

谢谢

【问题讨论】:

标签: ios swift3 ios10 unusernotificationcenter


【解决方案1】:

getPendingNotificationRequests 调用将一组请求传递给完成闭包。试试这样的:

let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { requests in
    for request in requests {
        print(request)
    }
})

【讨论】:

  • 在 ios 13 中它总是给我 0 .. 我使用的是设备而不是模拟器.. 对此有什么想法吗?
  • @Abhishek 你确定他们的安排正确吗?通知是否正确显示?
  • 通知正在正确安排。但获取请求数为 0。
  • @Abhishek 我遇到了同样的问题。你已经想通了吗?
  • 如何打印标题?它只是为所有人显示已编辑
【解决方案2】:

实际上不是现在,但您应该允许使用通知

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if !granted {
            print("user has declined notifications")
        }
    }

【讨论】:

    【解决方案3】:

    以防万一有人需要访问已经发送的通知(用户可以在通知中心看到它们),可以通过以下方式完成:

        let center = UNUserNotificationCenter.current()
        center.getDeliveredNotifications { notifications in
                // use center.removeDeliveredNotifications(withIdentifiers:requestIdsToRemove)
                // if you want to cancel some of the notifications displayed to user
            }
        }
    

    【讨论】:

      【解决方案4】:

      对于 ObjC:- [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray *requests){ NSLog(@"requests: %@", requests); }];

      【讨论】:

        猜你喜欢
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 2020-05-18
        • 2017-11-27
        • 1970-01-01
        • 2017-11-03
        相关资源
        最近更新 更多