zoukankan      html  css  js  c++  java
  • 本地通知UILocalNotification

    1、增加一个本地推送
    //设置20秒之后 

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];

        //chuagjian一个本地推送

        UILocalNotification *noti = [[[UILocalNotification allocinitautorelease];

        if (noti) {

            //设置推送时间

            noti.fireDate = date;

            //设置时区

            noti.timeZone = [NSTimeZone defaultTimeZone];

            //设置重复间隔

            noti.repeatInterval = NSWeekCalendarUnit;

            //推送声音

            noti.soundName = UILocalNotificationDefaultSoundName;

            //内容

            noti.alertBody = @"推送内容";

            //显示在icon上的红色圈中的数子

            noti.applicationIconBadgeNumber = 1;

            //设置userinfo 方便在之后需要撤销的时候使用

            NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];

            noti.userInfo = infoDic;

            //添加推送到uiapplication        

            UIApplication *app = [UIApplication sharedApplication];

            [app scheduleLocalNotification:noti];  

        }


    2、程序运行时接收到本地推送消息

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

    {

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"接收到本地提醒 in app"

    message:notification.alertBody

       delegate:nil

      cancelButtonTitle:@"确定"

      otherButtonTitles:nil];

    [alert show];

    //这里,你就可以通过notification的useinfo,干一些你想做的事情了

    application.applicationIconBadgeNumber -= 1;

    }


    3、取消一个本地推送

    UIApplication *app = [UIApplication sharedApplication];

        //获取本地推送数组

        NSArray *localArr = [app scheduledLocalNotifications];

        

        //声明本地通知对象

        UILocalNotification *localNoti;

        

        if (localArr) {

            for (UILocalNotification *noti in localArr) {

                NSDictionary *dict = noti.userInfo;

                if (dict) {

                    NSString *inKey = [dict objectForKey:@"key"];

                    if ([inKey isEqualToString:key]) {

                        if (localNoti){

                            [localNoti release];

                            localNoti = nil;

                        }

                        localNoti = [noti retain];

                        break;

                    }

                }

            }

            

            //判断是否找到已经存在的相同key的推送

            if (!localNoti) {

                //不存在 初始化

                localNoti = [[UILocalNotification allocinit];

            }

            

            if (localNoti && !state) {

                //不推送 取消推送

                [app cancelLocalNotification:localNoti];

                [localNoti release];

                return;

            }

    }



    1. 第一步:创建本地推送  
    2. // 创建一个本地推送  
    3. UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];  
    4. //设置10秒之后  
    5. NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];  
    6. if (notification != nil) {  
    7.     // 设置推送时间  
    8.     notification.fireDate = pushDate;  
    9.     // 设置时区  
    10.     notification.timeZone = [NSTimeZone defaultTimeZone];  
    11.     // 设置重复间隔  
    12.     notification.repeatInterval = kCFCalendarUnitDay;  
    13.     // 推送声音  
    14.     notification.soundName = UILocalNotificationDefaultSoundName;  
    15.     // 推送内容  
    16.     notification.alertBody = @"推送内容";  
    17.     //显示在icon上的红色圈中的数子  
    18.     notification.applicationIconBadgeNumber = 1;  
    19.     //设置userinfo 方便在之后需要撤销的时候使用  
    20.     NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];  
    21.     notification.userInfo = info;  
    22.     //添加推送到UIApplication         
    23.     UIApplication *app = [UIApplication sharedApplication];  
    24.     [app scheduleLocalNotification:notification];   
    25.      
    26. }  
    27.   
    28. 第二步:接收本地推送  
    29. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{  
    30.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    31.     [alert show];  
    32.     // 图标上的数字减1  
    33.     application.applicationIconBadgeNumber -= 1;  
    34. }  
    35.   
    36. 第三步:解除本地推送  
    37. // 获得 UIApplication  
    38. UIApplication *app = [UIApplication sharedApplication];  
    39. //获取本地推送数组  
    40. NSArray *localArray = [app scheduledLocalNotifications];  
    41. //声明本地通知对象  
    42. UILocalNotification *localNotification;  
    43. if (localArray) {  
    44.     for (UILocalNotification *noti in localArray) {  
    45.         NSDictionary *dict = noti.userInfo;  
    46.         if (dict) {  
    47.             NSString *inKey = [dict objectForKey:@"key"];  
    48.             if ([inKey isEqualToString:@"对应的key值"]) {  
    49.                 if (localNotification){  
    50.                     [localNotification release];  
    51.                     localNotification = nil;  
    52.                 }  
    53.                 localNotification = [noti retain];  
    54.                 break;  
    55.             }  
    56.         }  
    57.     }  
    58.      
    59.     //判断是否找到已经存在的相同key的推送  
    60.     if (!localNotification) {  
    61.         //不存在初始化  
    62.         localNotification = [[UILocalNotification alloc] init];  
    63.     }  
    64.      
    65.     if (localNotification) {  
    66.         //不推送 取消推送  
    67.         [app cancelLocalNotification:localNotification];  
    68.         [localNotification release];  
    69.         return;  
    70.     }  
    71. }  

  • 相关阅读:
    前端工程师们,这些干货让你开发效率加倍
    我的代码片段
    人生至少有一次为了自己的勇气而活
    美食篇之御桥小聚
    美食篇之好好对自己
    F
    Github 简明教程
    A
    完美字符串
    1222: FJ的字符串 [水题]
  • 原文地址:https://www.cnblogs.com/melons/p/5792015.html
Copyright © 2011-2022 走看看