如何在iOS远程通知上禁用默认通知警报视图?

时间:2017-02-22 07:03:13

标签: ios objective-c iphone notifications

在我的应用中,我启用了远程通知

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")){
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if( !error ){
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }];
}else{
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}

我实现了如下的委托方法,

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Failed!!");
}


 - (void)application:(UIApplication *)application   didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Print message.
    if (userInfo) {
        NSLog(@"Message ID: %@", userInfo);
    }

    completionHandler(UIBackgroundFetchResultNoData);
}

但是当应用程序在前台并收到通知时,我得到了一个带有通知标题和消息的UIAlertView。我想

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"User Info : %@",notification.request.content.userInfo);
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionBadge);
}

//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center       didReceiveNotificationResponse:(UNNotificationResponse *)response    withCompletionHandler:(void(^)())completionHandler{
    NSLog(@"User Info : %@",response.notification.request.content.userInfo);
    completionHandler();
}

我在willPresentNotification:上进行了调试,在此触发后我收到了警报。我也通过删除此方法进行测试。但是在收到通知时仍会显示此警报。

如何禁用此警报视图?我在iPhone上使用10.2 iOS

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为UIAlertController不是来自通知。你确定你没有在其他地方创建吗?

您的通知实施似乎没问题。

您可以在项目中搜索并使用断点查看是否有任何UIAlertController被点击?

答案 1 :(得分:0)

在完成处理程序中使用UNNotificationPresentationOptionNone

int(,16)