码迷,mamicode.com
首页 > 其他好文 > 详细

本地推送笔记

时间:2016-04-09 16:37:34      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

一. 本地推送

1.添加本地通知 -》添加提醒任务

  >在ios8.0以后,添加本地通知,要添加权限

    // 添加本地通知权限

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:

                                            UIUserNotificationTypeAlert|

                                            UIUserNotificationTypeBadge|

                                            UIUserNotificationTypeSound

                     categories:nil];

    [application registerUserNotificationSettings:settings];

 

  >掌握当程序是否杀死的情况下,本地通知调用的两个方法

    程序未杀死的情况:调用

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

    程序杀死的情况:调用

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

   > "如果通知添加重复时间,这个通知永远在系统里"

2. 具体代码

 1 #import "AppDelegate.h"
 2 @interface AppDelegate ()
 3 @end
 4 
 5 @implementation AppDelegate
 6 
 7 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 8     // Override point for customization after application launch.
 9     // 添加本地通知权限
10     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];
11     [application registerUserNotificationSettings:settings];
12     
13     // 如果程序被杀死 点击通知信息 会来到这个方法
14     if (launchOptions) {
15         NSString *msg = [NSString stringWithFormat:@"%@",launchOptions];
16         UILocalNotification *localNoti = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
17         NSString *info = localNoti.userInfo[@"info"];
18         [self showAlert:info];
19     }
20     return YES;
21 }
22 
23 #pragma mark 接收本地通知,当程序杀死的时候,不会调用这个方法
24 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
25     // 获取额外通知信息
26     NSString *info = notification.userInfo[@"info"];
27     [self showAlert:info];
28     
29     // 取消单个通知
30 //    [application cancelLocalNotification:notification];  
33 }

36 -(void)showAlert:(NSString *)msg{ 37 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xxxx" message:msg delegate:nil cancelButtonTitle:@"知道" otherButtonTitles:nil, nil]; 38 39 [alert show]; 40 } 41 @end

 

#import "ViewController.h"

@interface ViewController ()
- (IBAction)addBrithdayNotif:(id)sender;
@end

@implementation ViewController
// 添加生日提醒
- (IBAction)addBrithdayNotif:(id)sender {
    // 假设 某某 生日 是明天
    
    // 1.创建一个本地通知
    UILocalNotification *localNoti = [[UILocalNotification alloc] init];
    
    // 2.设置 通知内容
    localNoti.alertBody= @"明天老婆大人生日";
    
    
    // 3.设置 提醒时间 (通知触发的时间)
    
    // 2015-07-20 14:00:00 -> NSDate
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//    localNoti.fireDate = [df dateFromString:@"2015-07-20 14:00:00"];
    
    localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
 
    // 添加通知额外信息
    localNoti.userInfo = @{@"info":@"明天下班去拿花,然后xx晚餐"};
    
    // 设置通知重复的时间间隔
#warning 只能添加重复时间,这个通知永远在系统里
//    localNoti.repeatInterval = NSCalendarUnitMinute;
    
    // 设置通知启动图片
#warning 有LunchScreen.xib时候,启动图片无效
    localNoti.alertLaunchImage = @"Default";
    
    // 4.执行本地通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
}

- (IBAction)getLocalNoti:(id)sender {
    // 现在正在执行本地通知
    NSLog(@"%@", [UIApplication sharedApplication].scheduledLocalNotifications);
}

- (IBAction)cancelLocalNoti:(id)sender {
    // 取消本地通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

 

 

 

本地推送笔记

标签:

原文地址:http://www.cnblogs.com/Life-GJB/p/5371794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!