1.申请开启推送通知
// IOS8 新系统需要使用新的代码咯
if (miOS8Later)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
//ios7注册推送通知
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
2.检查推送是否开启
-(BOOL)isAllowedNotification {
if (miOS8Later) {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone != setting.types) {
return YES;
}
} else {
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone != type)
return YES;
}
return NO;
}
3.推送本地通知
- (void)registerLocalNotification:(NSTimeInterval)alertTime pushString:(NSString*)pushString {
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval = 0;
notification.alertBody = pushString;
notification.applicationIconBadgeNumber = 1;
notification.soundName = UILocalNotificationDefaultSoundName;
if(mLLDebugFastSetting){
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"text" forKey:@"text"];
notification.userInfo = userDict;
}
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}