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下运行图标:

                       

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

     

  • 相关阅读:
    Centos 7 安装jdk 配置环境变量
    【转载】C#检测客户端输入的内容是否含有危险字符串
    【转载】C#将图片以二进制流的方式存入数据库
    【转载】为何你的网站一直不被百度、搜狗、谷歌等搜索引擎收录
    【转载】目前国内主流的云服务器厂商有哪些
    【转载】阿里云服务器为网站选配Https证书
    【转载】网站遭遇DDoS攻击怎么办
    使用的阿里云服务器被黑客入侵怎么办
    HTTP状态码以及其含义大全
    Url的Base64编码以及解码
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3679778.html
Copyright © 2011-2022 走看看