我在App Delegate中设置了本地通知使用此方法:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"Watch the Latest Episode of CCA-TV"];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序然后退出它时,我收到一条错误消息:
2014-06-07 11:14:16.663 CCA-TV [735:149070] 试图安排本地通知 {开火日期= 2014年6月7日星期六太平洋夏令时间11:14:21,时区= America/Los_Angeles (PDT)偏移-25200(日光),重复间隔= 0,重复计数= UILocalNotificationInfiniteRepeatCount,下一个开火日期= 2014年6月7日星期六太平洋夏令时间11:14:21,用户信息=(null)} 带警报但尚未收到用户显示警报的许可
如何获得显示警报的必要权限?
在我的应用程序中,我希望断言已以正确的格式添加通知.我通常会使用依赖注入来执行此操作,但我无法想到测试新UNUserNotificationCenterAPI的方法.
我开始创建一个模拟对象来捕获通知请求:
import Foundation
import UserNotifications
class NotificationCenterMock: UNUserNotificationCenter {
var request: UNNotificationRequest? = nil
override func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)? = nil) {
self.request = request
}
}
Run Code Online (Sandbox Code Playgroud)
但是,UNUserNotificationCenter没有可访问的初始化程序,我无法实例化模拟.
我甚至不确定我是否可以通过添加通知请求和获取当前通知来进行测试,因为测试需要在模拟器上请求权限以阻止测试.目前我已经将通知逻辑重构为包装器,所以我至少可以在整个应用程序中模拟它并手动测试.
我有比手动测试更好的选择吗?