1.必须先注册才能用
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"用户同意");
}
else
{
NSLog(@"用户拒绝");
}
}];
2.循序协议,设置代理,实现方法
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = BKGetStringWithKeyFromTable(@"你的账号已在其他地方登陆,本地已经下线", nil);
content.sound = [UNNotificationSound defaultSound];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
[self loginOut];
}];
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
completionHandler();
}
网友评论