菜鸟教程小白 发表于 2022-12-12 23:34:09

iOS 10/9 推送通知


                                            <p><p>我需要更新我们的应用程序以支持 iOS 9 和 iOS 10,所以我的问题是使用 <em>UNUserNotificationCenter</em> 进行推送通知。</p>

<p>因此,在 iOS 9 中,我们有一个方法可以返回 <em>UIUserNotificationSettings</em> 的结果,例如</p>

<pre><code>- (BOOL)alertEnabled {
    UIUserNotificationSettings *theSettings = [ currentUserNotificationSettings];
    return &amp; UIUserNotificationTypeAlert;
}
</code></pre>

<p>在 iOS 10 中,我做了类似的事情</p>

<pre><code>- (void)userNotificationsAuthorization :(void (^)(BOOL alertIsActive))completion {
    [ getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
      completion(settings.alertSetting == UNNotificationSettingEnabled);
    }];
}
</code></pre>

<p>调用它并通过完成处理程序获取它。</p>

<p>我的问题:是否有可能使用 <em>getNotificationSettingsWithCompletionHandler</em> 和 <strong>return</strong> 值而不是完成处理程序,我可以将它用于我的 <em>alertEnabled</em > 方法?</p>

<p>非常感谢。</p>

<p>更新:</p>

<p>通过这种方法,它正在工作</p>

<pre><code>- (BOOL)alertIsEnabled {
    __block BOOL alertIsActive = NO;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@&#34;10.0&#34;)) {
      dispatch_semaphore_t sem = dispatch_semaphore_create(0);

      [ getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            alertIsActive = settings.alertSetting == UNNotificationSettingEnabled;
            dispatch_semaphore_signal(sem);
      }];

      dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    }
    else {
      UIUserNotificationSettings *theSettings = [ currentUserNotificationSettings];
      alertIsActive = &amp; UIUserNotificationTypeAlert;   
    }
    return alertIsActive;
}
</code></pre>

<p>但也许有更好的解决方案</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以,经过一周的测试,它对我来说效果很好。</p>

<p>针对我的具体问题,我创建了一个自定义类。</p>

<p><em>SpecicPush.h</em></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

typedef NS_ENUM(NSInteger , PushNotificationType) {
    PushNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    PushNotificationTypeBadge   = 1 &lt;&lt; 0, // the application may badge its icon upon a notification being received
    PushNotificationTypeSound   = 1 &lt;&lt; 1, // the application may play a sound upon a notification being received
    PushNotificationTypeAlert   = 1 &lt;&lt; 2, // the application may display an alert upon a notification being received
};

@interface SpecificPush : NSObject

@property (nonatomic, readonly) PushNotificationType currentNotificationSettings;

+ (SpecificPush *)sharedInstance;
- (PushNotificationType)types;

@end
</code></pre>

<p><em>SpecificPush.m</em></p>

<pre><code>#import &#34;SpecificPush.h&#34;
#import &lt;UserNotifications/UserNotifications.h&gt;

@interface SpecificPush()
@property (nonatomic) PushNotificationType currentNotificationSettings;
@end

@implementation SpecificPush

#pragma mark - Init

static SpecificPush *instance = nil;
+ (SpecificPush *)sharedInstance
{
    @synchronized (self)
    {
      if (instance == nil)
      {
            ;
      }
    }
    return instance;
}

- (instancetype)init
{
    NSAssert(!instance, @&#34;WARNING - Instance of SpecifishPush already exists&#34;);
    self = ;
    if (self)
    {
      self.currentNotificationSettings = PushNotificationTypeNone;
    }

    instance = self;
    return self;
}

- (PushNotificationType)types
{
    if (IS_IOS10)
    {
      dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

      [ getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
            if ((settings.soundSetting == UNNotificationSettingDisabled) &amp;&amp; (settings.alertSetting == UNNotificationSettingDisabled) &amp;&amp; (settings.soundSetting == UNNotificationSettingDisabled))
            {
                self.currentNotificationSettings = PushNotificationTypeNone;
            }
            if (settings.badgeSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeBadge;
            }
            if (settings.soundSetting == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeSound;
            }

            if (settings.alertStyle == UNNotificationSettingEnabled)
            {
                self.currentNotificationSettings = PushNotificationTypeAlert;
            }

            dispatch_semaphore_signal(semaphore);

      }];

      dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
      dispatch_release(semaphore);
    }
    else
    {
      UIUserNotificationSettings *settings = [ currentUserNotificationSettings];

      if (settings.types == UIUserNotificationTypeNone)
      {
            self.currentNotificationSettings = PushNotificationTypeNone;
      }
      if (settings.types &amp; UIUserNotificationTypeBadge)
      {
            self.currentNotificationSettings = PushNotificationTypeBadge;
      }
      if (settings.types &amp; UIUserNotificationTypeSound)
      {
            self.currentNotificationSettings = PushNotificationTypeSound;
      }
      if (settings.types &amp; UIUserNotificationTypeAlert)
      {
            self.currentNotificationSettings = PushNotificationTypeAlert;
      }
    }

    return self.currentNotificationSettings;
}
@end
</code></pre>

<p>我使用了与 UIUserNotificationType 相同的 NS_ENUM。现在我可以轻松使用旧的实现了。 </p>

<pre><code>- (BOOL)alertEnabled {
    UIUserNotificationSettings *theSettings = [ currentUserNotificationSettings];
    return &amp; UIUserNotificationTypeAlert;
}
</code></pre>

<p>我用</p>

<pre><code>- (BOOL)alertEnabled {
    return [ types] &amp; PushNotificationTypeAlert;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 10/9 推送通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41564543/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41564543/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 10/9 推送通知