codebot
codebot

Reputation: 2656

How to disable default notification alert view on iOS remote notifications?

In my app I enabled remote 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];
}

And I implemented the delegate methods as follows,

- (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);
}

But when the app is in foreground and receiving a notification, I got a UIAlertView with the notification title and message. I want to

//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();
}

I debugged on willPresentNotification: and after this triggers I got the alert. And I tested by removing this method as well. But still this alert is showing when receiving a notification.

How may I disable this alert view? I'm using 10.2 iOS in my iPhone

enter image description here

Upvotes: 3

Views: 936

Answers (2)

Jonathan García
Jonathan García

Reputation: 1353

Use UNNotificationPresentationOptionNone in the completionHandler

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"User Info : %@",notification.request.content.userInfo);
    completionHandler(UNNotificationPresentationOptionNone);
}

Upvotes: 0

user7219266
user7219266

Reputation:

I don't think the UIAlertController comes from notifications. Are you sure you are not creating one elsewhere ?

Your notifications' implementation seems ok.

Can you search in your project and using breakpoints to see if any of your UIAlertControllers get hit?

Upvotes: 1

Related Questions