本地推送

第一步在AppDelegate.h进行设置

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    /*
     
     UIUserNotificationTypeNone    = 0,      // 没有本地通知
     
     UIUserNotificationTypeBadge   = 1 << 0, //  接受图标右上角提醒数字
     
     UIUserNotificationTypeSound   = 1 << 1, //  接受通知时候,可以发出音效
     
     UIUserNotificationTypeAlert   = 1 << 2, //  接受弹窗 (横幅、弹窗)
     
     
     */
    
    
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    
    [application registerUserNotificationSettings:settings];
    
    
    return YES;
}

/**
 *  如果应用在后台,通过点击通知的时候打开应用会来到这个代理方法
 *  如果应用在前台,接受到本地通知就会调用该方法
 *  @param notification 通过哪一个通知来这里
 */

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    
    
    if (application.applicationState == UIApplicationStateActive)
        
        return ;
    if (application.applicationState == UIApplicationStateInactive) {
        
        NSLog(@"跳转");
    }
        
    
    
    
    
}

 第二步在ViewController.h里

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated{

//消除右上角的提醒数 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } -(void)viewWillDisappear:(BOOL)animated{ } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* @property(nonatomic,copy) NSDate *fireDate; //设置本地推送的时间 @property(nonatomic,copy) NSTimeZone *timeZone; //时区() @property(nonatomic) NSCalendarUnit repeatInterval; //重复多少个单元发出一次 @property(nonatomic,copy) NSCalendar *repeatCalendar; //设置日期 @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); // 进入某个区域的时候发出通知 @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); // 进入区域发送的通知是否重复 @property(nonatomic,copy) NSString *alertBody; //推送消息的内容 @property(nonatomic) BOOL hasAction; //是否显示alertAction的文字(默认YES) @property(nonatomic,copy) NSString *alertAction; // 设置锁屏状态下,显示的文字 @property(nonatomic,copy) NSString *alertLaunchImage; // 显示启动图片 @property(nonatomic,copy) NSString *alertTitle NS_AVAILABLE_IOS(8_2); // @property(nonatomic,copy) NSString *soundName; //设置音效 默认 UILocalNotificationDefaultSoundName @property(nonatomic) NSInteger applicationIconBadgeNumber; // 应用图标右上角的提醒数字 @property(nonatomic,copy) NSDictionary *userInfo; // @property (nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0); */ - (IBAction)loginBtn:(UIButton *)sender { //1.创建本地通知 UILocalNotification *localNote = [[UILocalNotification alloc]init]; //1.1设置什么时候弹出 localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; //1.2设置弹出的内容 localNote.alertBody = @"吃饭了吗?"; //1.3设置锁屏状态下,显示的一个文字 localNote.alertAction = @"快点打开"; //1.4是否显示alertAction的文字(默认YES) localNote.hasAction = NO ; //1.5显示启动图片 localNote.alertLaunchImage = @"111"; //1.6设置音效 localNote.soundName = UILocalNotificationDefaultSoundName ; //1.7应用图标右上角的提醒数字 localNote.applicationIconBadgeNumber = 99 ; //1.8设置UserInfo来传递信息 localNote.userInfo = @{@"alertBody":localNote.alertBody,@"applicationIconBadgeNumber":@(localNote.applicationIconBadgeNumber)}; //2.调度通知 [[UIApplication sharedApplication] scheduleLocalNotification:localNote]; }
原文地址:https://www.cnblogs.com/yyxblogs/p/4835447.html