测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年5月 |
由 Héctor Marqués 维护。
MRLocalNotificationFacade
是一个类,它封装了处理 iOS 本地通知所需的大多数 API
UIUserNotificationSettings
对象。- (void)registerForNotificationWithBadges:(BOOL)badgeType alerts:(BOOL)alertType sounds:(BOOL)soundType categories:(NSSet *)categories;
- (BOOL)isBadgeTypeAllowed;
- (BOOL)isSoundTypeAllowed;
- (BOOL)isAlertTypeAllowed;
// etc.
NSError
对象。- (BOOL)scheduleNotification:(UILocalNotification *)notification withError:(NSError **)errorPtr;
- (UIAlertController *)buildAlertControlForError:(NSError *)error;
// application:didRegisterUserNotificationSettings:
- (void)handleDidRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
// application:didReceiveLocalNotification:
- (void)handleDidReceiveLocalNotification:(UILocalNotification *)notification;
// application:handleActionWithIdentifier:forLocalNotification:completionHandler:
- (void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler;
// etc.
UIApplicationStateActive
状态时显示本地通知。- (UIAlertController *)buildAlertControlForNotification:(UILocalNotification *)notification;
- (void)showAlertController:(UIAlertController *)alert;
- (NSDate *)buildDateWithDay:(NSInteger)day month:(NSInteger)month year:(NSInteger)year hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;
- (NSDate *)getGMTFireDateFromNotification:(UILocalNotification *)notification;
// etc.
- (UILocalNotification *)buildNotificationWithDate:(NSDate *)fireDate timeZone:(BOOL)timeZone category:(NSString *)category userInfo:(NSDictionary *)userInfo;
- (UILocalNotification *)buildNotificationWithRegion:(CLRegion *)fireRegion triggersOnce:(BOOL)regionTriggersOnce category:(NSString *)category userInfo:(NSDictionary *)userInfo;
- (UIMutableUserNotificationAction *)buildAction:(NSString *)identifier title:(NSString *)title destructive:(BOOL)isDestructive backgroundMode:(BOOL)runsInBackground authentication:(BOOL)authRequired;
- (UIMutableUserNotificationCategory *)buildCategory:(NSString *)identifier minimalActions:(NSArray *)minimalActions defaultActions:(NSArray *)defaultActions;
// etc.
将 MRLocalNotificationFacade 目录复制到您的项目中。
如果您不使用 MRLocalNotificationFacade,则基本上会做同样的事情,但是使用它 ;)
首先从应用程序代理方法中调用 MRLocalNotificationFacade 处理程序
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
[notificationFacade setContactSuportURLWithEmailAddress:@"[email protected]"];
UILocalNotification *notification = [notificationFacade getNotificationFromLaunchOptions:options];
[notificationFacade handleDidReceiveLocalNotification:notification];
return YES;
}
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
[notificationFacade handleDidReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)app didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
[notificationFacade handleDidRegisterUserNotificationSettings:settings];
}
- (void)application:(UIApplication *)app handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())handler {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
[notificationFacade handleActionWithIdentifier:identifier forLocalNotification:notification completionHandler:handler];
}
@end
然后您只需注册您在最佳方便时通知用户的首选选项即可
- (IBAction)registerForNotificationsAction:(id)sender {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
NSSet *categories = ...; // use notificationFacade for creating your action groups
[notificationFacade registerForNotificationWithBadges:YES alerts:YES sounds:YES categories:categories];
}
您只需继续创建通知并使用 MRLocalNotificationFacade
中提供的多个 build*
和 customize*
方法来安排它即可
- (void)scheduleNotification:(NSString *)text date:(NSDate *)date category:(NSString *)category {
MRLocalNotificationFacade *notificationFacade = MRLocalNotificationFacade.defaultInstance;
UILocalNotification *notification = [notificationFacade buildNotificationWithDate:date
timeZone:NO
category:category
userInfo:nil];
[notificationFacade customizeNotificationAlert:notification
title:nil
body:text
action:nil
launchImage:nil];
NSError *error;
BOOL scheduled = [notificationFacade scheduleNotification:notification
withError:&error];
if (error && scheduled) {
// if the user needs to change settings, the recovery attempter will handle this
UIAlertController *alert = [notificationFacade buildAlertControlForError:error];
[notificationFacade showAlertController:alert];
} else {
// this is bad, maybe you prefer to do something else...
UIAlertController *alert = [notificationFacade buildAlertControlForError:error];
[notificationFacade showAlertController:alert];
}
}
即使您已经在处理应用程序的本地通知,您也可以使用 MRLocalNotificationFacade 检查您的本地通知对象的有效性
NSError *error;
BOOL canSchedule = [notificationFacade canScheduleNotification:notification
withRecovery:YES
error:&error];
if (canSchedule && error) {
// user needs to change settings or the app has to register notification's category
UIAlertController *alert = [notificationFacade buildAlertControlForError:error];
[notificationFacade showAlertController:alert];
} else if (!canSchedule) {
// notification is not valid
NSAssert(NO, @"unhandled error: %@", error);
}
MRLocalNotificationFacade 提供 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。