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

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

    // 设置触发通知的时间

    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:4];

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

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZone defaultTimeZone];

    // 设置重复的间隔

    notification.repeatInterval = kCFCalendarUnitSecond;

    // 通知内容

    notification.alertBody =  @"美肤计划开始了!";

    notification.applicationIconBadgeNumber = 1;

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

    notification.soundName = UILocalNotificationDefaultSoundName;

    // 通知参数

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

    notification.userInfo = userDict;

    // ios8后,需要添加这个注册,才能得到授权

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type

                                                                                 categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        // 通知重复提示的单位,可以是天、周、月

        notification.repeatInterval = NSCalendarUnitDay;

    } else {

        // 通知重复提示的单位,可以是天、周、月

        notification.repeatInterval = NSCalendarUnitSecond;

    }

    // 执行通知注册

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    在delegate中回调方法

    // 本地通知回调函数,当应用程序在前台时调用

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

        NSLog(@"noti:%@",notification);

        

        // 这里真实需要处理交互的地方

        // 获取通知所带的数据

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

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

                                                        message:notMess

                                                       delegate:nil

                                              cancelButtonTitle:@"OK"

                                              otherButtonTitles:nil];

        [alert show];

        // 更新显示的徽章个数

        NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

        badge--;

        badge = badge >= 0 ? badge : 0;

        [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

        

        // 在不需要再推送时,可以取消推送

        [HomeViewController cancelLocalNotificationWithKey:@"key"];

    }

    //本地通知

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

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

        // 获取所有本地通知数组

        NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;

        

        for (UILocalNotification *notification in localNotifications) {

            NSDictionary *userInfo = notification.userInfo;

            if (userInfo) {

                // 根据设置通知参数时指定的key来获取通知参数

                NSString *info = userInfo[key];

                

                // 如果找到需要取消的通知,则取消

                if (info != nil) {

                    [[UIApplication sharedApplication] cancelLocalNotification:notification];

                    break;

                }

            }

        }

    }

    设置的时间到了以后,会自动在桌面弹出一个提示框,点显示后,就可以启动软件。然后在

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

    {

        UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

        if (localNotif)

        {

            NSLog(@"Recieved Notification %@",localNotif);

            NSDictionary* infoDic = localNotif.userInfo;

            NSLog(@"userInfo description=%@",[infoDic description]);

            NSString* codeStr = [infoDic objectForKey:CODE];

        }

    }里,对lanchOptions进行处理,找到它里面的信息,就可以拿到设置时的需要处理的东西,就可以继续操作了。

    如果此时你的客户端 软件仍在打开,则会调用

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif

    {

        

    }

    一样的处理方法。

  • 相关阅读:
    html添加注释怎么弄?
    编程语言本身是怎么开发出来的?
    一句话说明Facbook React证书的矛盾点
    XAMPP是什么?
    HTTP解析
    version control
    函数式编程语言
    Servlet之Filter
    Build tool
    container和injection
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5019842.html
Copyright © 2011-2022 走看看