本地通知LocalNotification简单实用及示例

 iOS推送通知

    1、注意:这里说的推送通知跟NSNotification有所区别

    NSNotification是抽象的,不可见的

    推送通知是可见的(能用肉眼看到)


    2、iOS中提供了2种推送通知

    本地推送通知(Local Notification)

    远程推送通知(Remote Notification)


    3、推送通知的作用

    让app不在前台时,告示用户App的最新情况

    

    4、推送通知的呈现方式

    在屏幕顶部一块横幅

    在屏幕中间弹出UIAlertView

    同时可以播放音效

    锁屏时展示

    App图标右上角额数字提示

    5、本地推送通知(具体实现)

 

/**

 *   注册通知  

 */

+ (void)registerLocalNotification:(NSInteger)time content:(NSString *)content key:(NSString *)key;

{

   

        //创建一个本地通知

    UILocalNotification *notification = [[UILocalNotification alloc] init];


    // 设置触发通知的时间

    //需要使用时间戳

    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:time];

  

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZone defaultTimeZone];

    // 设置重复的间隔

    notification.repeatInterval = 0;//0表示不重复

    // 通知内容

    notification.alertBody =  content;

    //应用程序右上角 角标

    notification.applicationIconBadgeNumber = 1;

    // 通知被触发时播放的声音

    notification.soundName = UILocalNotificationDefaultSoundName;

    // 通知参数

//    NSDictionary *userDict = [NSDictionary dictionaryWithObject:content forKey:key];

    notification.userInfo = userDict;

    //选择使用 哪个操作组

    notification.category = @"select";

    //请求本地通知 授权

    [self getRequestWithLocalNotificationSleep:notification];

}


- (void)getRequestWithLocalNotificationSleep:(UILocalNotification *)notification

{

    // ios8后,需要添加这个注册,才能得到授权

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }

    

#warning 注册完之后如果不删除,下次会继续存在,即使从模拟器卸载掉也会保留

    

    //删除之前的通知

    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    


    // 执行通知注册

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];


}

    

/**

 *   取消通知

 */

+ (void)cancelLocalNotificationWithKey:(NSString *)key

{

    // 获取所有本地通知数组

    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;

    

    if (localNotifications) {

        

        

        for (UILocalNotification *notification in localNotifications) {

            NSDictionary *userInfo = notification.userInfo;

            if (userInfo) {

                // 根据设置通知参数时指定的key来获取通知参数

                NSString *info = userInfo[key];

                

                // 如果找到需要取消的通知,则取消

                if ([info isEqualToString:key]) {

                    if (notification) {

                        [[UIApplication sharedApplication] cancelLocalNotification:notification];

                    }

                    break;

                }

            }

        }

        

    }

}

当用户点击本地推送通知,会自动打开app,这里有2种情况

1》app并没有关闭,一直隐藏在后台,接受通知回调

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

2》app已经kill,则会调用AppDelegate的下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

例如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

   

   //创建一个本地通知

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    // 设置触发通知的时间

    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZone defaultTimeZone];

    // 设置重复的间隔

    notification.repeatInterval = 0;//0表示不重复

    // 通知内容

    notification.alertBody =  @“邯郸市发生”;

    // 通知被触发时播放的声音

    notification.soundName = UILocalNotificationDefaultSoundName;

    // 通知参数

    notification.userInfo = @{@"selectIndex" : @(1)};

//删除之前的通知

    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    // 执行通知注册

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

#pragma mark - 退出程序后

    // 处理退出后通知的点击,程序启动后获取通知对象,如果是首次启动还没有发送通知,那第一次通知对象为空,没必要去处理通知(如跳转到指定页面)

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

        UILocalNotification *localNotifi = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

        [self changeLocalNotifi:localNotifi];

    }

    return YES;

}

- (void)changeLocalNotifi:(UILocalNotification *)localNotifi{

    // 如果在前台直接返回,如果此时还在前台可能会弹出两次

    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {

        return;

    }

    // 获取通知信息

    NSString *selectIndex = localNotifi.userInfo[@"selectIndex"];

    // 获取根控制器TabBarController

    UITabBarController *rootController = (UITabBarController *)self.window.rootViewController;

    // 跳转到指定控制器

    rootController.selectedIndex = [selectIndex intValue];

}

6、ios8之后可以自定义推送按钮优化,但是在Ios10已经废除。也就是说ios8 9 会有效果

//1.创建消息上面要添加的动作(按钮的形式显示出来)

    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];

    action.identifier = @"action";//按钮的标示

    action.title=@"接收";//按钮的标题

    action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序

    //    action.authenticationRequired = YES;

    //    action.destructive = YES;

    

    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];

    action2.identifier = @"action2";

    action2.title=@"拒绝";

    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理

    action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;

    action.destructive = YES;


//2.创建动作(按钮)的类别集合

    UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];

    categorys.identifier = @"alert";//这组动作的唯一标示

    [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];


//3.创建UIUserNotificationSettings,并设置消息的显示类类型

    UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];


//4.注册推送

    [[UIApplication sharedApplication] registerForRemoteNotifications];

    [[UIApplication sharedApplication] registerUserNotificationSettings:uns];


//5.发起本地推送消息

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];

    notification.timeZone=[NSTimeZone defaultTimeZone];

    notification.alertBody=@"测试推送的快捷回复";

    notification.category = @"alert";//注意此处的标识要与设置的标识显示一致

    [[UIApplication sharedApplicationscheduleLocalNotification:notification];

当点击行为时,会执行以下方法:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler

7、至于ios10----------------UserNotifications后续再接上。

你可能感兴趣的:(OC)