2014-02-05 22 views

回答

1

我不這樣做,所以我們可以檢測到UIAlertView按鈕用戶按下,因爲沒有任何類型的iOS中提供的回調方法或委託等。

只有當您按Don't Allow這將禁用該特定iOS應用程序的推送通知服務,如果是,則啓用。

然後通過代碼,我們可以檢查並確保使用它。

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
if (types == UIRemoteNotificationTypeNone) 
    // NONE 
+0

感謝您的答案,但我正在尋找一些方法來檢測「不允許」按鈕點擊事件。 – sanjana

+1

@sanjana您無法檢測到「不允許」事件,但您可以使用上述代碼根據您的應用程序未被允許接收APN來執行某些操作。 – sangony

+0

@sangony是的,這是我的想法。謝謝 – Buntylm

0
iOS8 comes with rregisterUserNotificationSettings: delegate method. Using this method we can do some patches.Please review them and Let us know your comments.Please these is only work with iOS8. 

=>添加/註冊的通知。

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

    } 

=>這裏下面的方法被稱爲After Alert Notification Fire。使用任何按鈕操作(不允許或允許)我們強制完全註冊通知。和道瓊斯在這裏有一些補丁。

#ifdef __IPHONE_8_0 

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 
    //register to receive notifications 
    [application registerForRemoteNotifications]; 
} 

=>我們做一些伎倆在這裏

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken { 

    NSLog(@"devToken: %@",devToken); 

#if !TARGET_IPHONE_SIMULATOR 
    NSString* deviceToken = [[[[[devToken description] 
           stringByReplacingOccurrencesOfString: @"<" withString: @""] 
           stringByReplacingOccurrencesOfString: @">" withString: @""] 
           stringByReplacingOccurrencesOfString: @" " withString: @""] retain]; 

    NSLog(@"deviceToken : %@",deviceToken); 

    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 

    if((![[standardDefaults valueForKey:@"DeviceToken"] isEqualToString:deviceToken]) || [standardDefaults valueForKey:@"DeviceToken"]==nil){ 
     [self sendProviderDeviceToken:deviceToken]; 
    }else{ 
     //Do Some Stuff Here 
    } 
} 
14

我敢肯定有人會需要一個堅實的,簡單的答案,這(就像我曾經做過) - 所以在這裏你去。調用[[UIApplication sharedApplication] registerForRemoteNotifications];後直接就可以使用NSNotificationCenter漂亮,像這樣:

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification 
                object:nil 
                queue:[NSOperationQueue mainQueue] 
               usingBlock:^(NSNotification * _Nonnull note) { 
                if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { 
                 //user tapped "Allow" 
                } 
                else{ 
                 //user tapped "Don't Allow" 
                } 
               }]; 

注:我的設備正在運行iOS 9.2,我的Xcode是7.2版本,我的部署目標爲8.0。

+0

我相信它會起作用,但我仍然感受到它的一種破壞:( – sanjana

+0

正是我在尋找的東西,像一個魅力! – kleezy