iOS 本地推送

来源:互联网 发布:龙腾世纪起源知乎 编辑:程序博客网 时间:2024/05/29 21:16
1.首先在一个类#import"TrainingEvaluatiOrder.h"中声明两个类方法

// 设置本地通知

+ (void)registerLocalNotification:(NSInteger)alertTime;

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

2.在.m文件中实现两个方法,可以创建button点击事件实现调用 + (void)registerLocalNotification:(NSInteger)alertTime;这个方法

// 设置本地通知alertTime为多久以后执行本地推送

+ (void)registerLocalNotification:(NSInteger)alertTime {

    UILocalNotification *notification = [[UILocalNotificationalloc]init];

    // 设置触发通知的时间

    NSDate *fireDate = [NSDatedateWithTimeIntervalSinceNow:alertTime];

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

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZonedefaultTimeZone];

    // 设置重复的间隔

    notification.repeatInterval =kCFCalendarUnitSecond;

    // 通知内容

    notification.alertBody@"本地推送";

    notification.applicationIconBadgeNumber =1;

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

    notification.soundName =UILocalNotificationDefaultSoundName;

    // 通知参数

    NSDictionary *userDict = [NSDictionarydictionaryWithObject:@"本地推送实现"forKey:@"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;

            }

        }

    }

}

3.在Appdelegate的

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;中调用

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

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

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"本地通知(前台)"

                                                    message:notMess

                                                   delegate:nil

                                          cancelButtonTitle:@"OK"

                                          otherButtonTitles:nil];

    [alert show];

    // 更新显示的徽章个数

    NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;

    badge--;

    badge = badge >= 0 ? badge :0;

    [UIApplicationsharedApplication].applicationIconBadgeNumber = badge;

在调用+ (void)cancelLocalNotificationWithKey:(NSString *)key;这个方法


0 0
原创粉丝点击