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

iOS 10 本地推送UNUserNotificationCenter

2016-12-09 10:42 411 查看

一:首先在AppDelegate.m中注册本地通知

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];

二:发送推送消息

-(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
WCLog(@"接收到消息----%@",message);
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
WCLog(@"------------在后台");
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:message.fromStr arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:message.body
arguments:nil];
content.sound = [UNNotificationSound defaultSound];

// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger];

// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

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