本地推送
AppDelegate.m
注册:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:setting];
}
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
}
return YES;
}
发送通知:
- (IBAction)addLocalNotification:(id)sender {
//判断是否已经授权用户发送通知
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
// 创建本地通知对象
UILocalNotification *localNotifi = [UILocalNotification new];
// 设置属性
// 时间
localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
// 内容
localNotifi.alertBody = @"今天不适合敲代码";
// 声音
localNotifi.soundName = UILocalNotificationDefaultSoundName;
// 标记
localNotifi.applicationIconBadgeNumber = 5;
// 调度本地通知
[[UIApplication sharedApplication]scheduleLocalNotification:localNotifi];
} else{
//用户注册通知,注册后才能收到通知,这会给用户一个弹框,提示用户选择是否允许发送通知
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
}
远程推送
程序推倒后台或是退出,会和应用后台服务器断开链接,收不到信息,但只要设备联网就可以苹果的APNs(Apple Push Notification service)服务器建立链接,依然可以收到远程推送的消息
步骤1: