您的位置:首页 > 移动开发 > IOS开发

iOS: 本地通知&远程推送

2017-09-22 17:47 495 查看

本地推送

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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: