iOS开发之旅--本地通知的发送与取消

来源:互联网 发布:网络市场调研案例 编辑:程序博客网 时间:2024/05/29 08:29

     现在有时间,就把之前学习的使用过的本地通知的基本过程给大家分享一下。

    解释一下本地通知是程序员在应用中给用户手动推送的消息。(与远程推送的具体区别,后期会更新。)

     本地通知的具体实现如下:


   首先, 我创建了一个本地通知的Demo:AmerLocationNotification:

    在Main.storyboard中拖一个switch并脱线出Change事件:


    接下来,我们要将具体实现写在该事件中。

- (IBAction)changed:(UISwitch *)sender{    UIApplication *app = [UIApplication sharedApplication];        if (sender.on)    {        // 初始化本地通知对象        UILocalNotification *notification = [[UILocalNotification alloc] init];                if (notification)        {            // 使用本地时区            notification.timeZone = [NSTimeZone defaultTimeZone];                        //设置出发时间            notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];                        // 设置重复间隔            notification.repeatInterval = kCFCalendarUnitMinute;                        // 设置锁屏时,提醒文字title            notification.alertAction = @"查水表";                        // 设置提醒的文字内容            notification.alertBody   = @"你妈妈叫你回家吃饭";                        // 通知提示音 使用默认的            notification.soundName= UILocalNotificationDefaultSoundName;                        // 设置应用程序右上角的提醒个数            notification.applicationIconBadgeNumber = 1;                        // 设定通知的userInfo,用来标识该通知            NSDictionary *infor = @{@"amer.org" : @"key"};                        notification.userInfo = infor;            /*这里也可以设置alertLaunchImage*/                        // 将通知添加到系统中              [app scheduleLocalNotification:notification];        }    }    else    {        //获取所有除与调度中的数组        NSArray *locationArr = app.scheduledLocalNotifications;        if (locationArr)        {            for (UILocalNotification *ln in locationArr)            {                NSDictionary *dict =  ln.userInfo;                                if (dict)                {                    NSString *obj = [dict objectForKey:@"amer.org"];                                        if ([obj isEqualToString:@"key"])                    {                        //取消调度通知                        [app cancelLocalNotification:ln];                    }                }            }        }    }}

   注意:这种状态的通知,是只有你的应用进入后台才会有提示。

              else中得代码是要取消通知,一定要实现,否则你会被不停的通知烦死。因为如果你不取消通知,即使你将应用卸载重装,通知依旧存在,仍然会给你弹。


下一步,我们要实现让通知在应用在前台显示时也可以提醒,代码位于appDelegate中.

 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{     NSLog(@"接收到通知");        [[[UIAlertView alloc] initWithTitle:notification.alertAction                               message:notification.alertBody                              delegate:nil                     cancelButtonTitle:@"确定"                     otherButtonTitles:nil] show];        application.applicationIconBadgeNumber = 0;}- (void)applicationWillEnterForeground:(UIApplication *)application{    //如果app在前台运行    application.applicationIconBadgeNumber = 0;}

最后最重要的一步,千万不要漏掉,否则你什么通知也收不到。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        UIApplication *app = [UIApplication sharedApplication];    //是否响应registerUserNotificationSettings(ios8以后)    if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])    {        UIUserNotificationSettings *settings = [UIUserNotificationSettings                                                settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];                [app registerUserNotificationSettings:settings];    }    else       //如果不适配iOS8之前的设备,这个else可以省略    {        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge |                                           UIRemoteNotificationTypeAlert |                                           UIRemoteNotificationTypeSound;                [app registerForRemoteNotificationTypes:myTypes];    }    return YES;}-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{        [application registerForRemoteNotifications]; // <================== 新添加        NSLog(@"接收到通知");        [[[UIAlertView alloc] initWithTitle:notification.alertAction                               message:notification.alertBody                              delegate:nil                     cancelButtonTitle:@"确定"                     otherButtonTitles:nil] show];        application.applicationIconBadgeNumber = 0;} 

文档原话:

// In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:]   before being able to schedule and present UILocalNotifications


0 0
原创粉丝点击