iOS日常需求:UILocalNotification本地推送的使用详解

来源:互联网 发布:xp系统查找mac地址 编辑:程序博客网 时间:2024/05/22 06:57

UILocalNotification在实际的项目使用中用的还是比较多的,刚好在项目中用了一下,发现一些问题总结出来跟大家分享一下!

1、首先封装本地推送的设置和退出方法

+ (void)registerLocalNotification:(NSInteger)alertTime Sting:(NSString* )str{

    UILocalNotification *notification = [[UILocalNotificationalloc]init];

    //设置触发通知的时间

   NSDate *fireDate = [NSDatedateWithTimeIntervalSinceNow:alertTime];

   NSLog(@"fireDate=%@",fireDate);

    

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZonedefaultTimeZone];

    // 设置重复的间隔

    notification.repeatInterval =kCFCalendarUnitSecond;

    

    // 通知内容

    notification.alertBody =  str;

    notification.applicationIconBadgeNumber =1;

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

    notification.soundName =UILocalNotificationDefaultSoundName;

    // 通知参数

   NSDictionary *userDict = [NSDictionarydictionaryWithObject:strforKey:@"key"];

    notification.userInfo = userDict;

    

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

   if ([[UIApplicationsharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        UIUserNotificationType type = UIUserNotificationTypeAlert |UIUserNotificationTypeBadge |UIUserNotificationTypeSound;

        UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:type

                                                                                categories:nil];

        [[UIApplicationsharedApplication]registerUserNotificationSettings:settings];

        //通知重复提示的单位,可以是天、周、月

        notification.repeatInterval =NSCalendarUnitDay;

    }else {

        //通知重复提示的单位,可以是天、周、月

        notification.repeatInterval =NSDayCalendarUnit;

    }

    

    // 执行通知注册

    [[UIApplicationsharedApplication]scheduleLocalNotification:notification];

}


// 取消某个本地推送通知

+ (void)cancelLocalNotificationWithKey:(NSString *)key {

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

    NSArray *localNotifications = [UIApplicationsharedApplication].scheduledLocalNotifications;

    

   for (UILocalNotification *notificationin localNotifications) {

       NSDictionary *userInfo = notification.userInfo;

       if (userInfo) {

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

           NSString *info = userInfo[key];

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

           if (info !=nil) {

                [[UIApplicationsharedApplication]cancelLocalNotification:notification];

               break;

            }

        }

    }

}

2、在AppDelegate中引入封装好的类的头文件

在didFinisLaunchingWithOptions函数中注册本地通知的背景数字和提示声音

[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];

3、在本地推送的响应函数中实现弹出内容

// 本地通知回调函数,当应用程序在前台时调用

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

    //这里真实需要处理交互的地方

    //获取通知所带的数据

   NSString *notMess = [notification.userInfoobjectForKey:@"key"];

    MozTopAlertView* topView = [MozTopAlertViewshowWithType:MozAlertTypeInfotext:notMessparentView:self.window.viewForBaselineLayout];

    UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(topAlertAction)];

    [topView addGestureRecognizer:tap];

    //更新显示的徽章个数

    NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;

    badge--;

    badge = badge >=0 ? badge :0;

    [UIApplicationsharedApplication].applicationIconBadgeNumber = badge;

   UIButton* btn = [[UIButtonalloc]init];

    //在不需要再推送时,可以取消推送

    [PXHHomeNotificationcancelLocalNotificationWithKey:@"key"];

}

4、然后在触发事件中调用本地推送事件

[classNameregisterLocalNotification:0Sting:currentContent];//数字为响应时间间隔   string为显示内容

5、这样就可以看到响应的屏幕显示效果了


0 0