簡體   English   中英

如何檢測用戶是否在Apple的推送通知確認提醒中點擊“不允許”

[英]How to detect if user tap on “Don't Allow” on Apple's push notification confirmation alert

如果用戶點擊蘋果推送通知提醒消息上的“不允許”按鈕,我想發出一些事件。 是否有任何通知被觸發或以其他方式檢測用戶的此操作?

我相信有人會需要一個堅實而簡單的答案(就像我曾經做過的那樣) - 所以你走了。 調用[[UIApplication sharedApplication] registerForRemoteNotifications];后直接調用[[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。

我不能這樣我們可以檢測UIAlertView按鈕用戶按下了什么,因為在iOS中沒有提供任何類型的回調方法或委托等。

僅當您按下Don't Allow才會禁用該特定iOS應用程序的推送通知服務,如果是,則啟用。

之后通過代碼我們可以檢查並確保使用它。

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   // NONE
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]];

    }

=> 此處下面的方法稱為警報通知火災后。 使用任何按鈕操作(不允許或允許)我們強制完全注冊通知。 和陶氏在這里有一些補丁。

#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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM