MUUserNotifications 1.1

MUUserNotifications 1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布日期最新发布日期2016年11月

muer2000维护。



  • muer

MUUserNotifications API类似于iOS 10的UserNotifications.framework

用法

用户授权

NSMutableArray *actions = [NSMutableArray array];

MUNotificationAction *action1 = [MUNotificationAction actionWithIdentifier:@"action1-identifier" title:@"action1-title" options:MUNotificationActionOptionNone];
[actions addObject:action1];

MUNotificationAction *action2 = [MUNotificationAction actionWithIdentifier:@"action2-identifier" title:@"action2-title" options:MUNotificationActionOptionDestructive | MUNotificationActionOptionForeground];
[actions addObject:action2];

// iOS 9 text input action
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_4) {
    MUNotificationAction *inputAction = [MUTextInputNotificationAction actionWithIdentifier:@"inputAction-identifier" title:@"input-title" options:MUNotificationActionOptionNone textInputButtonTitle:@"Send"];
    [actions addObject:inputAction];
}

MUNotificationCategory *category = [MUNotificationCategory categoryWithIdentifier:@"category-identifier" actions:actions];

[[MUUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:MUUNAuthorizationOptionBadge | MUUNAuthorizationOptionAlert | MUUNAuthorizationOptionSound categories:[NSSet setWithObject:category] completionHandler:^(BOOL granted, NSError * _Nullable error) {
    NSLog(@"requestAuthorizatio granted: %d, error: %@", granted, error);
}];

授权状态 & 授权选项

typedef NS_ENUM(NSInteger, MUUNAuthorizationStatus) {
    MUUNAuthorizationStatusNotDetermined = 0,
    MUUNAuthorizationStatusDenied,
    MUUNAuthorizationStatusAuthorized
};
[MUUserNotificationCenter authorizationStatus];

typedef NS_OPTIONS(NSUInteger, MUUNAuthorizationOptions) {
    MUUNAuthorizationOptionNone     = 0,
    MUUNAuthorizationOptionBadge    = 1 << 0,
    MUUNAuthorizationOptionSound    = 1 << 1,
    MUUNAuthorizationOptionAlert    = 1 << 2,
    MUUNAuthorizationOptionCarPlay  = 1 << 3
};
[MUUserNotificationCenter authorizationOptions];

远程通知

  • 请求授权并注册接收远程通知(默认选项)
[MUUserNotificationCenter registerRemoteNotifications];
  • 自定义选项
[[MUUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:MUUNAuthorizationOptionAlert categories:[NSSet setWithObject:category] completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        [[MUUserNotificationCenter currentNotificationCenter] registerForRemoteNotifications];
    }
}];

本地通知

MUMutableNotificationContent *mutableContent = [[MUMutableNotificationContent alloc] init];
mutableContent.body = @"body...";
mutableContent.categoryIdentifier = @"category-identifier";
mutableContent.userInfo = @{@"key1": @"value1", @"key2": @"value2"};
mutableContent.sound = [MUNotificationSound defaultSound];
mutableContent.badge = @6;

MUNotificationTrigger *trigger = nil;
if (IS_IOS10_OR_GREATER) {
    trigger = [MUTimeIntervalNotificationTrigger triggerWithTimeInterval:6 repeats:NO];
}
else {
    trigger = [MUClassicNotificationTrigger triggerWithFireDate:[[NSDate date] dateByAddingTimeInterval:6] repeatInterval:0];
}

MUNotificationRequest *request = [MUNotificationRequest requestWithIdentifier:[[NSUUID UUID] UUIDString] content:mutableContent trigger:trigger];

[[MUUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    NSLog(@"addNotificationRequest erro: %@", error);
}];

统一的回调接口

用于接收通知和处理操作的统一代理接口。

注意:iOS 10必须开启“推送通知”才能使用远程推送功能