MRLocalNotificationFacade 2.0.0

MRLocalNotificationFacade 2.0.0

测试已测试
语言语言 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;
  • 处理 App 代理方法的职位.
// 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.

Notification example

入门学习

安装

手动

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 文件。