本地推送

APPDelegate.h


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

{

// 如果应用程序在前台,将应用程序图标上红色徽标中数字设为0

application.applicationIconBadgeNumber = 0;

// 使用UIAlertView显示本地通知的信息

[[[UIAlertView alloc] initWithTitle:@"收到通知"

message:notification.alertBody

delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];

}

ViewController.M   拖拽按钮


@interface ViewController (){

UIApplication *app;

}

@end


- (void)viewDidLoad {

[super viewDidLoad];

app = [UIApplication sharedApplication];

}


UISwitch *swith = (UISwitch *)sender;

if (swith.on) {

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

}

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

//设置一个通知触发时间

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

//设置一个通知时区

notification.timeZone = [NSTimeZone defaultTimeZone];

// 设置通知的重复发送的事件间隔

notification.repeatInterval = kCFCalendarUnitHour;

// 设置通知的声音

notification.soundName = @"gu.mp3";

//通知标题

notification.alertTitle=@"洪恩英语就是牛逼";

// 设置当设备处于锁屏状态时,显示通知的警告框下方的title

notification.alertAction = @"打开";

// 设置通知是否可显示Action

notification.hasAction = YES;

// 设置通过通知加载应用时显示的图片

notification.alertLaunchImage = @"logo.png";

// 设置通知内容

notification.alertBody = @"洪恩英语NB !";

// 设置显示在应用程序上红色徽标中的数字

notification.applicationIconBadgeNumber = 1;

NSDictionary *info = @{@"bys":@"key"};

notification.userInfo = info;

[app scheduleLocalNotification:notification];

}else{

NSArray *arr = [app scheduledLocalNotifications];

if (arr) {

for (UILocalNotification * noti in arr)

{

NSDictionary *dic = noti.userInfo;

if (dic) {

NSString *str = [dic objectForKey:@"key"];

if([str isEqualToString:@"bys"])

{

[app cancelLocalNotification:noti];

}

}

}

}

}

你可能感兴趣的:(本地推送)