本地通知

来源:互联网 发布:大连软件职业学院 编辑:程序博客网 时间:2024/05/21 17:19

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();
}

0 0
原创粉丝点击