2016-11-15 74 views
0

我收到了關於UNUserNotificationSettings的問題。我們可以使用任何方法在iOs 10+中打開/關閉通知(聲音,警報,徽章)嗎?在iOs 9及以下版本中,我使用registerUserNotificationSettings方法打開應用程序中的聲音,警報,徽章,但不能在iOs 10+中執行同樣的操作。對我的情況有何建議?與UNUserNotificationSettings相關的問題

回答

1

爲iOS 10,可以使用方法requestAuthorizationWithOptions這樣的:

//iOS 10 
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { 
    if (!error) { 
     NSLog(@"request authorization succeeded!"); 
    } 
}]; 

這是參數options的定義:

typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) { 
    UNAuthorizationOptionBadge = (1 << 0), 
    UNAuthorizationOptionSound = (1 << 1), 
    UNAuthorizationOptionAlert = (1 << 2), 
    UNAuthorizationOptionCarPlay = (1 << 3), 
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0); 
+0

感謝你的小費,但該方法解決不了我的問題 –

+0

@AD你想在授權後開啓/關閉嗎? – isaced

+0

是的,這正是我想要做的。我打算使用此方法: [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings){ }]; 但它只是獲取設置狀態,無法更新設置 –