IOS 8 本地推送补充
备注自己写的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
ScrollContentViewController * scrollcontentView = [[ScrollContentViewController alloc]init];
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:scrollcontentView];
_window.rootViewController = navigationController;
UIView * statusBarView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, 320, 64)];
statusBarView.backgroundColor = [UIColor colorWithRed:0 green:122/255.0f blue:247/255.0f alpha:1];
[navigationController.navigationBar addSubview:statusBarView];
[application setStatusBarHidden:NO];
[application setStatusBarStyle:UIStatusBarStyleLightContent];
//注册推送(ios 8)
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//接受本地推送
NSLog(@"%@",notification);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定"otherButtonTitles: nil];
[alert show];
//图标上的数字减1
application.applicationIconBadgeNumber -=1;
//解除本地推送
//获得uiapplication
UIApplication * app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray * localArray = [app scheduledLocalNotifications];
//声明本体通知对象
UILocalNotification * localNotification;
if (localArray)
{
for (UILocalNotification * noti in localArray)
{
NSDictionary * dict = noti.userInfo;
if (dict)
{
NSString * inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"对应的key值"])
{
if (localNotification)
{
localNotification = nil;
}
break;
}
}
}
//判断是否找到已经存在的相同key的推送
if (!localNotification)
{
//不存在初始化
localNotification = [[UILocalNotification alloc]init];
}
if (localNotification)
{
//不推送 取消推送
[app cancelLocalNotification:localNotification];
return;
}
}
}
.....
- (void)viewDidLoad
{
.....
}
-(void)SendNotification:(UIButton *)sender
{ //创建本地推送
NSDate * now = [NSDate date];
UILocalNotification * reminderNotification = [[UILocalNotification alloc]init];
//设置推送时间
[reminderNotification setFireDate:[now dateByAddingTimeInterval:10]];
//设置时区
[reminderNotification setTimeZone:[NSTimeZone defaultTimeZone]];
//设置userinfo 方便在之后需要撤销的时候使用
reminderNotification.userInfo = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
//设置推送内容
[reminderNotification setAlertBody:@"Don't forget to Show Out !"];
[reminderNotification setAlertAction:@"Show Out"];
[reminderNotification setCategory:@"alert"];
//设置推送声音
[reminderNotification setSoundName:UILocalNotificationDefaultSoundName];
//显示在icon上的红色圈子的数子
[reminderNotification setApplicationIconBadgeNumber:1];
//添加推送到UIApplication
[[UIApplication sharedApplication]scheduleLocalNotification:reminderNotification];
NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication]currentUserNotificationSettings]);
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications ];
UIAlertView * successAlert = [[UIAlertView alloc]initWithTitle:@"Reminder" message:@"Your Reminder has been Scheduled" delegate:nilcancelButtonTitle:@"OK Thanks ! " otherButtonTitles: nil];
[successAlert show];
}
IOS8定位问题
|