关闭

ios本地通知

75人阅读 评论(0) 收藏 举报
分类:

UILocalNotification常用属性

 

 fireDate;          通知的触发时间

 timeZone;          通知所在时区,如果指定时区,可以自行调整时间

 alertBody;         通知的正文

 soundName;         通知播放的声音,如果用UILocalNotificationDefaultSoundName,使用系统默认声音

 applicationIconBadgeNumber; 应用程序图标

 userInfo;          数据字典



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

{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                             categories:nil]];
        
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        //ios7注册推送通知
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }


    // 截获本地通知,用户通过通知的横幅点击进入系统,字典中才能找到本地通知

    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    

    // 判断通知是否存在

    if (notification) {

        NSLog(@"%@", notification.userInfo);

        [notification.userInfo writeToFile:@"/users/apple/Desktop/123.plist" atomically:YES];

    }

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    

    

    [self.window makeKeyAndVisible];

    return YES;

}


#pragma mark - 进入前台,清除右上角图标

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    [application setApplicationIconBadgeNumber:0];

}


#pragma mark - 进入后台

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    [self loadNotification];

}


#pragma mark - 创建本地通知

- (void)loadNotification

{

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

    // 5秒钟之后触发

    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    notification.alertBody = @"出大事了";

    notification.soundName = UILocalNotificationDefaultSoundName;

    notification.applicationIconBadgeNumber = 100;

    

    notification.userInfo = @{@"userName": @"马大哈", @"age": @(18)};

    

    // 要调用本地通知,需要通过UIApplication来统一调度

    [[UIApplication sharedApplication]scheduleLocalNotification:notification];

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74361次
    • 积分:3006
    • 等级:
    • 排名:第11719名
    • 原创:233篇
    • 转载:38篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论