初始化本地推送
- (void)initLocationNotification
{
//初始化
UILocalNotification *locationNotification = [[UILocalNotification alloc] init];
//设置推送时间,这里使用相对时间,如果fireDate采用GTM标准时间,timeZone可以至nil
locationNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
locationNotification.timeZone = [NSTimeZone defaultTimeZone];
//设置重复周期
locationNotification.repeatInterval = NSWeekCalendarUnit;
//设置通知的音乐
locationNotification.soundName = UILocalNotificationDefaultSoundName;
//设置通知内容
locationNotification.alertBody = @"推送测试--balabala";
//设置程序的Icon数量
locationNotification.applicationIconBadgeNumber = 1;
//执行本地推送
[[UIApplication sharedApplication] scheduleLocalNotification:locationNotification];
}
本地推送执行时会调用UIApplication的协议方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
在此方法中可以获取推送的内容
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"推送信息已经获得: %@",[notification alertBody]);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:[notification alertBody] delegate:self cancelButtonTitle:nil otherButtonTitles:@"good", nil] autorelease];
[alert show];
}
响应结果:
如果使用的是服务器推送,则会进入回调方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo