UILocalNotification本地推送

//远程推送 –> 服务器(第三方 umeng JPush(极光)) –> APNS

本地推送

Demo1
//获取推送权限

UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
NSLog(@"fireDate=%@",fireDate);

localNotification.fireDate = fireDate;
// 时区
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复的间隔
localNotification.repeatInterval = kCFCalendarUnitSecond;

// 通知内容
localNotification.alertBody =  @"该起床了...";
localNotification.applicationIconBadgeNumber = 1;
// 通知被触发时播放的声音
localNotification.soundName = UILocalNotificationDefaultSoundName;
// 通知参数
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@“哈哈哈” forKey:@"key"];
localNotification.userInfo = userDict;

// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

} 
// 执行通知注册
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

  • (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification {
    NSLog(@”noti:%@”,notification);

    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@”key”];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”本地通知(前台)”message:notMess
    delegate:nil
    cancelButtonTitle:@”OK”
    otherButtonTitles:nil];
    [alert show];

    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge–;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    // 在不需要再推送时,可以取消推送
    //[HomeViewController cancelLocalNotificationWithKey:@”key”];
    }

Demo2

//本地推送

//远程推送 –> 服务器(第三方 umeng JPush(极光)) –> APNS

扫描二维码关注公众号,回复: 3797973 查看本文章

@interface ViewController ()

@end

@implementation ViewController

-(void)localNotification
{
UILocalNotification *local = [[UILocalNotification alloc] init];

//发送的时间
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

//时区
local.timeZone = [NSTimeZone defaultTimeZone];

//设置发送的消息
local.alertBody = @"哈哈哈";

//设置应用程序的徽标
local.applicationIconBadgeNumber = 1;

//设置声音
local.soundName = UILocalNotificationDefaultSoundName;

//userInfo
local.userInfo = @{@"key":@"阿斯顿和父爱和发件客户卡拉好看哈克龙"};

//获取推送的权限
if([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

[[UIApplication sharedApplication] scheduleLocalNotification:local];

//取消本地推送
//[UIApplication sharedApplication] cancelLocalNotification:(nonnull UILocalNotification *)

}

猜你喜欢

转载自blog.csdn.net/weixin_35966617/article/details/53083744