IOS UILocalNotification 本地通知小例子

首先在AppDelegate.m中注册通知

//
//  AppDelegate.m
//  UILocalNotificationTest
//
//  Created by  Alex on 2017/4/4.
//  Copyright  2017年 alex. All rights reserved.
//

#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    return YES;
    
    // Override point for customization after application launch.
    //判断是否由远程消息通知触发应用程序启动
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
        //获取应用程序消息通知标记数(即小红圈中的数字)
        long badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
        if (badge>0) {
            //如果应用程序消息通知标记数(即小红圈中的数字)大于0,清除标记。
            badge--;
            //清除标记。清除小红圈中数字,小红圈中数字为0,小红圈才会消除。
            [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
        }
    }
    //消息推送注册
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        
        //iOS10特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        // 必须写代理,不然无法监听通知的接收与点击
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (granted) {
                
                // 点击允许
                
                NSLog(@"注册成功");
                
                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    
                    NSLog(@"%@", settings);
                    
                }];
                
            } else {
                
                // 点击不允许
                
                NSLog(@"注册失败");
                
            }
            
        }];
        
    }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
        
        //iOS8 - iOS10
        
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
        
        
        
    }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
        
        //iOS8系统以下
        
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
        
    }
    
    // 注册获得device Token
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

接着加一个点击事件触发通知

//
//  ViewController.m
//  UILocalNotificationTest
//
//  Created by  alex on 2017/4/4.
//  Copyright  2017年 alex. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  //  [self.mBtnSend addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchDragInside];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendNotify:(id)sender {
    [self SendLocalNotification];
}

- (void)send {
    
}

-(void)SendLocalNotification{
    //2.定制(预制)定制
    //2.1.创建本地通知
    UILocalNotification *local = [[UILocalNotification alloc]init];
    
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
    
    local.alertBody = @"女神:我们分手吧 ?";
    
    local.alertTitle = @"通知中心111";
    
    local.alertAction = @"XXXXX";
    
    local.hasAction = YES;
    
    local.soundName = UILocalNotificationDefaultSoundName;
    
    local.applicationIconBadgeNumber = 123;
    
    local.category = @"categorybiaoji";
    
    local.userInfo = @{@"name" : @"女神", @"QQ":@"110",@"sex" : @"女",@"age" : @"18"};
    
    
    //2.2定制
    [[UIApplication sharedApplication]scheduleLocalNotification:local];
    //3.展示 (自动展示)
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //2.定制(预制)定制
    //2.1.创建本地通知
    UILocalNotification *local = [[UILocalNotification alloc]init];
    
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
    
    local.alertBody = @"女神:我们分手吧 ?";
    
    local.alertTitle = @"通知中心111";
    
    local.alertAction = @"XXXXX";
    
    local.hasAction = YES;
    
    local.soundName = UILocalNotificationDefaultSoundName;
    
    local.applicationIconBadgeNumber = 99;
    
    local.category = @"categorybiaoji";
    
    local.userInfo = @{@"name" : @"女神", @"QQ":@"110",@"sex" : @"女",@"age" : @"18"};
    
    
    //2.2定制
    [[UIApplication sharedApplication]scheduleLocalNotification:local];
    //3.展示 (自动展示)
}

@end


第一次进入程序会有权限允许提示,如图:


当触发点击事件时候,按home键切换到后台,就会弹出本地通知了,如图:



好了又可以愉快的玩耍了,这里贴下项目小例子的地址:

点击打开链接

http://download.csdn.net/detail/msn465780/9827585

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值