UNUserNotificationCenter确实收到了响应,但从未将完成处理程序称为iOS11,objective-C

时间:2018-09-14 00:25:23

标签: objective-c unusernotificationcenter

有时我的一位用户报告说他没有收到应在何时发布的声音或横幅广告

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)ResponsewithCompletionHandler:(void(^)(void))completionHandler

    由系统调用。我有一个内部跟踪,该跟踪可在从App Store接收到我的应用的设备上运行,并确认未调用此方法。跟踪还显示已将多个(64)通知请求发布到系统。我已经对这个问题进行了广泛的搜索,所有建议的答案都反映了我在代码中的工作。这是appDelegate中设置通知中心和委托方法的代码(省略的无关代码由三个点表示)。对于解决此问题的任何帮助,我将不胜感激。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.
            NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
            UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    
            UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
            if (notification) {
    
                NSLog(@"application didFinishLaunching with UIApplicationLaunchOptionsLocalNotificationKey");
                [self applicationDidBecomeActive:application];
            }
    
    
            UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
            if (notification) {
                // run after a delay so that the cordova/kendo app has time to get ready
                //  [self performSelector:@selector(postNotification:) withObject:notification afterDelay:4.0];
                NSLog(@"application didFinishLaunching with UIApplicationLaunchOptionsLocalNotificationKey");
                [self applicationDidBecomeActive:application];
            }
    

...

  center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              // Enable or disable features based on authorization.
                              NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
                              if(granted == YES){
                                  [storage setBool:YES forKey:@"permission granted"];
                                  [storage setBool:YES forKey:@"alert permission granted"];
                                  [storage setBool:YES forKey:@"sound permission granted"];
                              }else{
                                  NSLog(@"No permission granted");
                                  [storage setBool:NO forKey:@"permission granted"];
                              };
                          }];

...

 #pragma mark UNNotificationCenter setup

    UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"ACCEPT_IDENTIFIER" title:NSLocalizedString(@"Continue notifications", nil) options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"DECLINE_IDENTIFIER" title:NSLocalizedString(@"Stop notifications", nil) options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction *doNotDisturbAction = [UNNotificationAction actionWithIdentifier:@"DO_NOT_DISTURB_IDENTIFIER" title:NSLocalizedString(@"Start Do Not Disturb", nil) options:UNNotificationActionOptionAuthenticationRequired];
    NSArray *actions = [NSArray arrayWithObjects:acceptAction, declineAction, doNotDisturbAction, nil];
    // NSArray *intentIdentifiers = [NSArray arrayWithObjects:@"none", nil];
    UNNotificationCategory *invite = [UNNotificationCategory categoryWithIdentifier:@"com.nelsoncapes.localNotification" actions:actions intentIdentifiers: @[] options:UNNotificationCategoryOptionNone];
    NSSet *categories = [NSSet setWithObjects:invite, nil];
    [center setNotificationCategories:categories];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              // Enable or disable features based on authorization.
                          }];

    [self setupWatchConnectivity];
    return YES

...

    #pragma mark UNNotification received in background
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"notification settings were changed");
        NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
        [storage setBool:YES forKey:KEventLoggerEventNotificationSettingsChanged];
        [storage synchronize];


        if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
            // Notifications not allowed
            NSLog(@"notification settings were changed");
            item.eventDescription = KEventLoggerEventNotificationsNotAllowed;

            // check settings for alert and sound
        }
       // UNNotificationSetting alertSetting = settings.alertSetting;
            if(settings.alertSetting == UNNotificationSettingEnabled){
                [storage setBool:YES forKey:@"alert permission granted"];
                item.eventDescription = KEventLoggerEventAlertsAreAllowed;
            }else{[storage setBool:NO forKey:@"alert permission granted"];
                item.eventDescription = KEventLoggerEventAlertsAreNotAllowed;
            }
            if (settings.soundSetting == UNNotificationSettingEnabled){
                [storage setBool:YES forKey:@"sound permission granted"];
                item.eventDescription = KEventLoggerEventSoundsAreAllowed;
            }else {[storage setBool:NO forKey:@"sound permission granted"];
                item.eventDescription = KEventLoggerEventSoundsAreNotAllowed;
            }

    }];
    NSLog(@"appdelegate - center didReceiveNotificationResponse");
    NSString *actionIdentifier = response.actionIdentifier;

    UNNotification *notification = response.notification;
    if([actionIdentifier isEqual:@"com.apple.UNNotificationDefaultActionIdentifier"] || [actionIdentifier isEqual:@"com.apple.UNNotificationDismissActionIdentifier"]){
    }else{


        BOOL accept = [actionIdentifier isEqual:@"ACCEPT_IDENTIFIER"];
        BOOL stop = [actionIdentifier isEqual:@"DECLINE_IDENTIFIER"];
        BOOL doNotDisturb = [actionIdentifier isEqual:@"DO_NOT_DISTURB_IDENTIFIER"];

        if (accept){NSLog(@"accept");
            [self handleAcceptActionWithNotification:notification];
        }
        else if (stop){NSLog(@"stop");
            [self handleDeclineActionWithNotification:notification];
        }
        else if(doNotDisturb) {NSLog(@"do not disturb");
            [self handleDoNotDisturbActionWithNotification:notification];
        };
    }

    completionHandler();
}

...

*#pragma mark UNNotification received in foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"appdelegate willPresentNotification");
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"notification settings were changed");
        NRCEventItem *item = [[NRCEventStore sharedStore]createItem];
        item.eventDateTime = [NSDate date];
        item.timeZone = [NSTimeZone localTimeZone];
        item.eventSender = [self description];

        NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];


        if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
            // Notifications not allowed
            NSLog(@"notification settings were changed");
            item.eventDescription = KEventLoggerEventNotificationsNotAllowed;
            // check settings for alert and sound
        }
        if(settings.alertSetting == UNNotificationSettingEnabled){
            [storage setBool:YES forKey:@"alert permission granted"];
            item.eventDescription = KEventLoggerEventAlertsAreAllowed;
        }else{[storage setBool:NO forKey:@"alert permission granted"];
            item.eventDescription = KEventLoggerEventAlertsAreNotAllowed;
        }
        if (settings.soundSetting == UNNotificationSettingEnabled){
            [storage setBool:YES forKey:@"sound permission granted"];
            item.eventDescription = KEventLoggerEventSoundsAreAllowed;
        }else {[storage setBool:NO forKey:@"sound permission granted"];
            item.eventDescription = KEventLoggerEventSoundsAreNotAllowed;
        }

    }];
    UNNotificationRequest * request = notification.request;
    NSString * actionIdentifier = request.identifier;
    if([actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier] || [actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]){
    }else{
            NSLog(@"app delegate notification received while in foreground");



            // remind the user
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notification", nil) message:@"Timer expired!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            NSLog(@"app delegate willPresentNotificationWithCompletionHandler: dnd is not started");
        }else{
            NSLog(@"app delegate willPresentNotificationWithCompletionHandler: dnd has started");
            [center removeAllPendingNotificationRequests];
            [center removeAllDeliveredNotifications];
        }
    }



    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);

}*

0 个答案:

没有答案