zoukankan      html  css  js  c++  java
  • UILocalNotification的学习

    示例代码:http://download.csdn.net/detail/chenscda/7230625

    介绍一下iOS下如何使用UILocalNotification进行应用程序的本地通知,基本上大部分的app都会有这个功能。

         我们在设置的通知中心中可以自定义本地通知的三种形式(分别是在ios6和ios7):

                           

    下面给出简单代码看看如何使用UILocalNotification:

    (1)本地通知中心发送消息:

    [cpp] view plaincopy
     
    1. UILocalNotification *notification=[[UILocalNotification alloc] init];  
    2.     if (notification!=nil) {  
    3.           
    4.         NSDate *now=[NSDate new];  
    5.         notification.fireDate=[now dateByAddingTimeInterval:6]; //触发通知的时间  
    6.         notification.repeatInterval=0; //循环次数,kCFCalendarUnitWeekday一周一次  
    7.           
    8.         notification.timeZone=[NSTimeZone defaultTimeZone];  
    9.         notification.soundName = UILocalNotificationDefaultSoundName;  
    10.         notification.alertBody=@"该去吃晚饭了!";  
    11.           
    12.         notification.alertAction = @"打开";  //提示框按钮  
    13.         notification.hasAction = YES; //是否显示额外的按钮,为no时alertAction消失  
    14.           
    15.         notification.applicationIconBadgeNumber = 1; //设置app图标右上角的数字  
    16.           
    17.         //下面设置本地通知发送的消息,这个消息可以接受  
    18.         NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
    19.         notification.userInfo = infoDic;  
    20.         //发送通知  
    21.         [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
    22.     }  


    (2)接受本地通知发送的消息(在AppDelegate文件中)

    [cpp] view plaincopy
     
    1. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{  
    2.       
    3.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    4.     [alert show];  
    5.       
    6.     NSDictionary* dic = [[NSDictionary alloc]init];  
    7.     //这里可以接受到本地通知中心发送的消息  
    8.     dic = notification.userInfo;  
    9.     NSLog(@"user info = %@",[dic objectForKey:@"key"]);  
    10.       
    11.     // 图标上的数字减1  
    12.     application.applicationIconBadgeNumber -= 1;  
    13. }  
    14.                               
    15. - (void)applicationWillResignActive:(UIApplication *)application  
    16. {  
    17.     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
    18.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
    19.       
    20.     // 图标上的数字减1  
    21.     application.applicationIconBadgeNumber -= 1;  
    22. }  


    下面给出ios7下运行图标:

                       

    这个内容比较简单,就不多说,详细的内容看看文档就可以了。大笑

     

  • 相关阅读:
    别让安全问题拖慢了 DevOps!
    精华阅读第 9 期 |滴滴出行 iOS 客户端架构演进之路
    微信小程序组件化开发框架WePY
    Web前端鼠标悬停实现显示与隐藏效果
    Web前端社交账号注册按钮
    微信小游戏2
    微信小游戏
    (2)
    Angular实战项目(1)
    一步HTML5教程学会体系
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3679778.html
Copyright © 2011-2022 走看看