美文网首页
iOS -- 本地通知

iOS -- 本地通知

作者: 您079 | 来源:发表于2017-12-19 10:45 被阅读0次

AppDelegate.m

- (void)application:(UIApplication *)application
    didReceiveLocalNotification:(UILocalNotification*)notification{
    // 如果应用程序在前台,将应用程序图标上红色徽标中数字设为0
    application.applicationIconBadgeNumber = 0;
    // 使用UIAlertView显示本地通知的信息
    [[[UIAlertView alloc] initWithTitle:@"收到通知"
        message:notification.alertBody
        delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
   // 应用程序再次进入前台时,将应用程序徽标设置0
   application.applicationIconBadgeNumber = 0;
}

Main.storyboard


Main storyboard.png

ViewController.m

定义成员变量

{
    UIApplication *app;    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    app = [UIApplication sharedApplication];
}

控件的点击事件方法

- (IBAction)changed:(id)sender;

实现控件的方法

- (IBAction)changed:(id)sender
{
    UISwitch* sw = (UISwitch*) sender;
    if (sw.on)
    {
        //判断当前的sdk是否可以调用registerUserNotificationSettings方法
        //
        if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
        }
        
        // 创建一个本地通知
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        // 设置通知的触发时间
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        // 设置通知的时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        // 设置通知的重复发送的事件间隔
        notification.repeatInterval = kCFCalendarUnitMinute;
        // 设置通知的声音
        notification.soundName = @"gu.mp3";
        //通知标题
        notification.alertTitle=@"标题标题标题";
        
        // 设置通知内容
        notification.alertBody = @"亲,好久不见,甚是想念!";
        // 设置显示在应用程序上红色徽标中的数字
        notification.applicationIconBadgeNumber = 1;
        // 设置userinfo,用于携带额外的附加信息。
        NSDictionary *info = @{@"ly": @"key"};
        notification.userInfo = info;
        // 调度通知
        [app scheduleLocalNotification:notification];  // ①
    }
    else
    {
        // 获取所有处于调度中本地通知数组
        NSArray *localArray = [app scheduledLocalNotifications];
        if (localArray)
        {
            for (UILocalNotification *noti in localArray)
            {
                NSDictionary *dict = noti.userInfo;
                if (dict)
                {
                    // 如果找到要取消的通知
                    NSString *inKey = [dict objectForKey:@"ly"];
                    if ([inKey isEqualToString:@"key"])
                    {
                        // 取消调度该通知
                        [app cancelLocalNotification:noti];  // ②
                    }
                }
            }
        }
    }
}

相关文章

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

  • iOS14开发- 通知

    iOS 中的通知主要分为 2 种,本地通知和远程通知。 本地通知 使用步骤 导入UserNotifications...

  • ios10新特性-UserNotification

    ios10新特性-UserNotification 引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是...

  • iOS UserNotifications通知管理---本地通知

    iOS UserNotifications通知管理---本地通知篇 iOS 10对以前混乱的和通知相关的API进行...

  • IOS的通知

    通知详解 简书-iOS10 推送通知 UserNotifications iOS10本地通知UserNotifi...

  • iOS推送通知概览

    iOS推送通知概览 一、响应推送(本地通知 和 远程通知 都合适) iOS 10 以前 1. UIUserNoti...

  • 关于iOS通知那些事

    一、概述 通知分为本地通知和远程推送通知,iOS10中对于通知这一块改变较大,本文主要针对iOS10的通知,iOS...

  • iOS 推送通知及通知扩展

    级别: ★★☆☆☆标签:「iOS 本地推送」「iOS 远程推送」「iOS通知扩展」作者: dac_1033审校: ...

  • iOS消息通知产品设计

    ios消息类型有本地通知、推送消息以及系统消息。 消息类型: 本地通知:是由ios设备生成并发布的,无论应用程序是...

  • 通知及Block传值代码示例

    通知 在IOS中,主要有广播通知(broadcast notification)、本地通知(local notif...

网友评论

      本文标题:iOS -- 本地通知

      本文链接:https://www.haomeiwen.com/subject/jpqbwxtx.html