本地消息提醒可與網絡數據爬取socket.io聯合使用
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {NSDate *now=[NSDate new];
//notification.fireDate=[now dateByAddingTimeInterval:2];//2秒後通知
//notification.repeatInterval=1;//循環次數,kCFCalendarUnitWeekday一週一次
notification.timeZone=[NSTimeZone defaultTimeZone];
//notification.applicationIconBadgeNumber=1; //應用的紅色數字
notification.soundName = UILocalNotificationDefaultSoundName;//聲音,可以換成alarm.soundName = @"myMusic.caf"
//去掉下面2行就不會彈出提示框
notification.alertBody="有消息來了";//提示信息 彈出提示框
notification.alertAction = @"打開"; //提示框按鈕
//notification.hasAction = NO; //是否顯示額外的按鈕,爲no時alertAction消失
// NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
//notification.userInfo = infoDict; //添加額外的信息
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
如果是在高版本的IOS中必須要加上這樣一段代碼纔可以實現消息提醒:
在AppDelegate.m文件的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }
方法體內加上:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}