【问题标题】:How do I ask for notifications permission in an IBAction?如何在 IBAction 中请求通知权限?
【发布时间】:2018-03-27 05:53:54
【问题描述】:

在我的入职培训中,我有一个 UIPageViewController,最后包含一个“入门”屏幕,用于授权通知。用户将点击一个标有“启用通知”的按钮,然后会出现通知权限对话框。我该如何做到这一点?

【问题讨论】:

    标签: ios objective-c permissions notifications apple-push-notifications


    【解决方案1】:

    你可以放:

    Objective-C

    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              // Enable or disable features based on authorization.
                          }];
    [[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
    

    斯威夫特

    let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification.
    

    在您的IBAction 中。

    请记住在你有IBAction的文件中添加import UserNotifications for Swift#import <UserNotifications/UserNotifications.h> for Objective-C,并确保Push Notification是在target - Capabilities - Push notification 下激活。

    【讨论】:

      【解决方案2】:

      目标-C:

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2020-12-19
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2017-07-31
      相关资源
      最近更新 更多