加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 请求用户在触发本地通知时显示警报

发布时间:2020-12-15 01:53:21 所属栏目:百科 来源:网络整理
导读:当本地通知被触发时,我想显示一个警报,但是为了让我得到许可,当我在iPhone上运行应用程序时对我说: Attempting to schedule a local notification {fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time,time zone = (null),
当本地通知被触发时,我想显示一个警报,但是为了让我得到许可,当我在iPhone上运行应用程序时对我说:

Attempting to schedule a local notification {fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time,time zone = (null),repeat interval = 0,repeat count = UILocalNotificationInfiniteRepeatCount,next fire date = Friday 13 June 2014 12 h 10 min 27 s Central European Summer Time,user info = (null)} with an alert but haven’t received permission from the user to display alerts

我怎样才能做到这一点?
现在的代码是:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:timeUntilNotification];
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.alertBody = @"ZEIT!";
    localNotif.alertAction = @"Show me the Timer!";
    localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

解决方法

添加此代码,它将显示警报视图以请求用户的权限。

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
                                                                                                          categories:nil]];
}

您可以在应用程序中添加此代码:didFinishLaunchingWithOptions;方法,以便应用程序会在您启动应用程序时询问用户,或者您可以在设置本地通知时添加此代码,这取决于您。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读