Gautam Shrivastav
Gautam Shrivastav

Reputation: 1248

I am getting notification as banner when my app is open. I don't want to show banner when my app is open

enter image description here

I am using Firebase Notification in my iOS app, where whenever I get notification, it shows as banner even when the app is open.

That doesn't actually happen when we use iOS native remote notification methods.

I tried to check if the default iOS methods are getting called but it is not working.

For registration:

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
            if( !error ){
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }
        }];
    }
    else{
        [application registerForRemoteNotifications];
    }

For Device Token:

- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
    // Note that this callback will be fired everytime a new token is generated, including the first
    NSString* deviceTkn = [[NSString stringWithFormat:@"%@",fcmToken] stringByReplacingOccurrencesOfString:@"<" withString:@""];
}

For receiving notification:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    //Called when a notification is delivered to a foreground app.
    NSDictionary *userInfo = notification.request.content.userInfo;
    completionHandler(UNNotificationPresentationOptionAlert);
}

Please help me out to resolve this

Upvotes: 3

Views: 340

Answers (1)

Mitesh
Mitesh

Reputation: 534

Here you set presentation style alert that's why showing banner. You should pass UNNotificationPresentationOptionNone in completion handler as per apple document.

https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter?language=objc

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    //Called when a notification is delivered to a foreground app.
    NSDictionary *userInfo = notification.request.content.userInfo;
    completionHandler(UNNotificationPresentationOptionAlert);
}

Upvotes: 2

Related Questions