如何检测用户是否点击"不允许"在Apple的推送通知确认提醒中

时间:2014-02-05 11:00:51

标签: ios ipad apple-push-notifications

如果用户点击苹果推送通知提醒消息中的“请勿允许”按钮,我想触发一些事件。是否有任何通知被触发或以其他方式检测用户的此操作?

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分:1)

我不能这样我们可以检测到用户按下了什么UIAlertView按钮,因为iOS中没有提供任何类型的回调方法或委托等。

只有按下Don't Allow这将禁用该特定iOS应用的推送通知服务,如果是,则启用。

然后通过代码,我们可以检查并确保使用。

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

答案 2 :(得分: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]];

    }

=> 此处下面的方法称为警报通知火灾后。使用任何按钮操作(不允许或允许)我们强制完全注册通知。和陶氏在这里有一些补丁。

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