本地推送消息

        iOS上有两种消息通知,一种是本地消息(Local Notification),一种是远程消息(PushNotification,也叫Remote Notification),设计这两种通知的目的都是为了提醒用户,现在有些什么新鲜的事情发生了,吸引用户重新打开应用。
  本地消息什么时候有用呢?譬如你正在做一个To-do的工具类应用,对于用户加入的每一个事项,都会有一个完成的时间点,用户可以要求这个To-do应用在事项过期之前的某一个时间点提醒一下它。为了达到这一目的,App就可以调度一个本地通知,在时间点到了之后发出一个Alert消息或者其他提示。

        主要介绍本地通知的注册以及使用,由于初学,不合理的地方还望指正。本地通知的使用主要分这么几个步骤:

步骤一:注册

在AppDelegate.m里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //注册本地推送
    if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication]registerUserNotificationSettings:settings];
    }

    return YES;
}

步骤二:添加

在添加通知的方法里

- (IBAction)addNotification:(UIButton *)sender
{
    if(!_flag)
    {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        //设置启动时间
        [localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
        //设置时间
        localNotification.timeZone = [NSTimeZone localTimeZone];
        //重复间隔
        localNotification.repeatInterval = kCFCalendarUnitMinute;
        //提醒的文字
        [localNotification setAlertBody:[NSString stringWithFormat:@"%@",self.inforText.text]];
        //播放声音
        [localNotification setSoundName:UILocalNotificationDefaultSoundName];
        
        localNotification.applicationIconBadgeNumber = 1;
        //参数信息,用于标识该通知
        NSDictionary *dictUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Alert",@"type", nil];
        localNotification.userInfo = dictUserInfo;
        //添加通知
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"flag"];
        _flag = YES;
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"通知添加成功,5秒后看效果" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"只能添加一个通知!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }
}

步骤三:处理

在AppDelegate.m里面

<span style="font-size:18px;"><span style="font-size:18px;">//当系统处于运行状态,收到通知可以弹出一个警告框</span></span>
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    application.applicationIconBadgeNumber = 0;
    [[[UIAlertView alloc]initWithTitle:@"通知" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]show];
}
//当系统从后台或非运行状态进入前台,会标置为0
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    application.applicationIconBadgeNumber = 0;
}

步骤四:取消

在取消通知的方法里

- (IBAction)cancelNotification:(UIButton *)sender
{
    if (_flag) {
        NSArray* arrayOfLocation = [[UIApplication sharedApplication] scheduledLocalNotifications];
        for (UILocalNotification* notify in arrayOfLocation) {
            if ([[notify.userInfo objectForKey:@"type"] isEqualToString:@"Alert"]) {
                [[UIApplication sharedApplication] cancelLocalNotification:notify];
            }
        }
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"flag"];
        _flag = NO;
        UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"通知取消成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }else{
        UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"没有通知可以取消" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值