2017-01-10 31 views
0

私の問題は、UNUserNotificationCenterをPushNotificationsに使用するために、iOS 9とiOS 10をサポートするためにアプリケーションを更新する必要があります。iOS 10/9プッシュ通知

だから、iOSの9で、私たちは私

- (void)userNotificationsAuthorization :(void (^)(BOOL alertIsActive))completion { 
    [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { 
     completion(settings.alertSetting == UNNotificationSettingEnabled); 
    }]; 
} 

のようなものは、それを呼び出し、完了ハンドラを経由して、それを手に入れたiOSの10で

- (BOOL)alertEnabled { 
    UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
    return [theSettings types] & UIUserNotificationTypeAlert; 
} 

ようUIUserNotificationSettingsの結果を返すメソッドを持っています。

私の質問:getNotificationSettingsWithCompletionHandler返り値の代わりに、私は方法をalertEnabled私にそれを使用することができます完了ハンドラを使用するには、いくつかの可能性はありますか?

ありがとうございます。

アップデート:この方法で

それは

- (BOOL)alertIsEnabled { 
    __block BOOL alertIsActive = NO; 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) { 
     dispatch_semaphore_t sem = dispatch_semaphore_create(0); 

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

     dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); 
    } 
    else { 
     UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
     alertIsActive = [theSettings types] & UIUserNotificationTypeAlert;  
    } 
    return alertIsActive; 
} 

を働いているが、おそらくよりよい解決策だから、

答えて

0

があり、テストの一週間後に、それは私のために正常に動作しています。

私の具体的な問題については、私はカスタムクラスを作成しました。

SpecicPush.h

#import <UIKit/UIKit.h> 

typedef NS_ENUM(NSInteger , PushNotificationType) { 
    PushNotificationTypeNone = 0,  // the application may not present any UI upon a notification being received 
    PushNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received 
    PushNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received 
    PushNotificationTypeAlert = 1 << 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 

SpecificPush.m

#import "SpecificPush.h" 
#import <UserNotifications/UserNotifications.h> 

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

@implementation SpecificPush 

#pragma mark - Init 

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

- (instancetype)init 
{ 
    NSAssert(!instance, @"WARNING - Instance of SpecifishPush already exists"); 
    self = [super init]; 
    if (self) 
    { 
     self.currentNotificationSettings = PushNotificationTypeNone; 
    } 

    instance = self; 
    return self; 
} 

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

     [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) { 
      if ((settings.soundSetting == UNNotificationSettingDisabled) && (settings.alertSetting == UNNotificationSettingDisabled) && (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 = [[UIApplication sharedApplication] currentUserNotificationSettings]; 

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

    return self.currentNotificationSettings; 
} 
@end 

私はUIUserNotificationTypeと同じNS_ENUMを使用。これで古い実装を簡単に使用できます。私は

- (BOOL)alertEnabled { 
    return [[SpecificPush sharedInstance] types] & PushNotificationTypeAlert; 
} 
を使用

- (BOOL)alertEnabled { 
    UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
    return [theSettings types] & UIUserNotificationTypeAlert; 
} 

のInsted