iOS应用开发 - 本地推送

AppDelegate.m(附注释)
 
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    //  注册本地通知,请求授权
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    

    //  当应用程序处于关闭状态
    //  一些界面的跳转和其他操作在该方法中执行
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
       

        //  获取推送参数
        UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        NSDictionary *userInfo= notification.userInfo;
        
        if (userInfo[@""] == ...) {
                  
              //  操作代码           
               ......
        }
    }
    
    return YES;
}

/*
 应用程序在进入前台,或者在前台的时候都会执行该方法
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
        // 针对应用程序在后台的时候进行的跳转
        
        if (notification.userInfo[@""] == ...) {
                  
              //  操作代码           
               ......
        }
}

@end
ViewController.m(附注释)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

/*
 //  还未进行了解的属性
 @property(nonatomic,copy) NSTimeZone *timeZone;
 @property(nonatomic) NSCalendarUnit repeatInterval;
 @property(nonatomic,copy) NSCalendar *repeatCalendar;
 @property(nonatomic,copy) CLRegion *region
 @property(nonatomic,assign) BOOL regionTriggersOnce NO
 
 */

- (IBAction)fireLocalNote:(id)sender {

    // 1.创建本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    
    // 2.设置本地通知的内容

        // 2.1.设置通知发出的时间
        localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
        // 2.2.设置通知的内容
        localNote.alertBody = @"填写通知内容";
        // 2.3.设置滑块的文字
        localNote.alertAction = @"锁屏状态滑块文字";
        // 2.4.决定alertAction是否生效
        localNote.hasAction = NO;
       // 2.5.设置点击通知的启动图片
        localNote.alertLaunchImage = @"随便填什么字符串都会默认显示启动图片";
        // 2.6.设置alertTitle
        localNote.alertTitle = @"alertTitle";
        // 2.7.设置有通知时的音效 默认为default
        localNote.soundName = @"sound.wav";
        // 2.8.设置应用程序图标右上角的数字
        localNote.applicationIconBadgeNumber = 100;
        // 2.9.设置额外信息
        localNote.userInfo = @{@"type" : @1};
 
    
    // 3.调用通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

@end

你可能感兴趣的:(iOS应用开发 - 本地推送)