本文主要是教你做推送的
本地推送
添加本地推送
///本地添加
-(void)addLocalPushNotification:(UIButton*)sender;
{
NSLog(@"%s",__FUNCTION__);
UILocalNotification* localNotification=[[UILocalNotification alloc]init];
if (localNotification) {
//设置时间当前加20秒
NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];
/*推送时区设置:从网上搜到
timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。*/
localNotification.timeZone=[NSTimeZone defaultTimeZone];
///推送时间设置
localNotification.fireDate=pushDate;
//时间间隔,若不设置将只会推送1次
localNotification.repeatInterval=kCFCalendarUnitDay;
//推送时的声音,(若不设置的话系统推送时会无声音)
localNotification.soundName=UILocalNotificationDefaultSoundName;
//推送的文字信息(若不设置,推送中心中不显示文字,有声音提示前提是设置有声音)
localNotification.alertBody=@"Hello world";
//推送时小图标的设置,PS:这个东西不知道还有啥用
localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
///这个东西,到时用于定位是哪个notification,以便取消用
NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotification.userInfo=infoDic;
//讲推送设置以及信息加入
UIApplication* app=[UIApplication sharedApplication];
BOOL status=YES;
for (UILocalNotification* notification in app.scheduledLocalNotifications) {
if ([notification.userInfo objectForKey:@"key"]) {
status=NO;
}
}
if (status) {
//加入推送(只能加入一次)
[app scheduleLocalNotification:localNotification];
}
NSLog(@"%@",app.scheduledLocalNotifications);
}
}
///本地移除
-(void)removeLocalPushNotification:(UIButton*)sender
{
NSLog(@"%s",__FUNCTION__);
UIApplication* app=[UIApplication sharedApplication];
//获取当前应用所有的通知
NSArray* localNotifications=[app scheduledLocalNotifications];
if (localNotifications) {
for (UILocalNotification* notification in localNotifications) {
NSDictionary* dic=notification.userInfo;
if (dic) {
NSString* key=[dic objectForKey:@"key"];
if ([key isEqualToString:@"name"]) {
//取消推送 (指定一个取消)
[app cancelLocalNotification:notification];
break;
}
}
}
}
//取消当前应用所有的推送
//[app cancelAllLocalNotifications];
}
远程推送