推送

AppleDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UILocalNotification *localNotifi = [UILocalNotification new];
    
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotifi];
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    
        
    
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    return YES;
}

ViewController.m
写一个button 添加一个事件

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn addTarget:self action:@selector(asd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

按钮的方法

- (void)asd{
    // 1.创建通知
    
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
        // 2.设置通知的必选参数
    
        // 设置通知显示的内容
    
        localNotification.alertBody = @"推送显示的信息";
    
        // 设置通知的发送时间,单位秒,在多少秒之后推送
    
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    
        //解锁滑动时的事件
    
        localNotification.alertAction = @"XXOOXX";
    
        //收到通知时App icon的角标
    
        localNotification.applicationIconBadgeNumber = 0;
    
        //推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName
    
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    
        //设置推送自定义声音格式
    
        //localNotification.soundName = @"文件名.扩展名";
    
        //循环次数
    
        localNotification.repeatInterval = kCFCalendarUnitDay;
    
        // 3.发送通知(根据项目需要使用)
    
        // 方式一: 根据通知的发送时间(fireDate)发送通知
    
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
}

猜你喜欢

转载自blog.csdn.net/weixin_42925415/article/details/82831570