0

I am creating an Ionic app with Angular 19 and using Capacitor. I am facing an issue only on iOS in web and android it works like a clock.

The code:

Login Component:

    notificationService = inject(NotificationService);    
    async signin() {
            if (this.loginForm.valid) {
              this.isLoading.set(true);
        
              const { isGranted, canRequest } =
                await this.notificationService.checkPermission();
        
              try {
                if (!isGranted && canRequest) {
                  this.isNotificationModalOpen.set(true);
                  const granted =
                    await this.notificationService.requestNotificationPermission();
                  if (!granted) {
                    // User denied notifications, proceed with login without token
                    await this.proceedWithLogin();
                    return;
                  }
                }
        
                // Get FCM token if permission is granted
                const fcmToken = await this.notificationService.getToken();
                await this.proceedWithLogin(fcmToken);
              } catch (error) {
                console.error('Error during login:', error);
                this.isLoading.set(false);
              }
            }
        }

Notifications Service:

     public async checkPermission(): Promise<{
        isGranted: boolean;
        canRequest: boolean;
      }> {
        if (this.isNative) {
          const permStatus = await PushNotifications.checkPermissions();
          return {
            isGranted: permStatus.receive === 'granted',
            canRequest: permStatus.receive === 'prompt',
          };
        } else {
          if (!('Notification' in window)) {
            return { isGranted: false, canRequest: false };
          }
    
          const permission = Notification.permission;
          return {
            isGranted: permission === 'granted',
            canRequest: permission === 'default',
          };
        }
      }

I am monitoring the error in Sentry and it is:

this.notificationService.checkPermission is not a function. (In 'this.notificationService.checkPermission()', 'this.notificationService.checkPermission' is undefined)

When signin() is called, nothing happens. It just keeps loading without signing in and I get the above error in Sentry Thanks in advance.

6
  • what does the constructor and import look like on the component that contains the signin function? Commented May 8 at 23:26
  • no constructor just " notificationService = inject(NotificationService);" and the import is "import { NotificationService } from '@shared/services/notification.service';" Commented May 9 at 10:09
  • I have tried to use the old injection in angular: " constructor(private notificationService: NotificationService) {}" also didn't work Commented May 9 at 10:10
  • did you add @Injectable decorator in your NotificationService ? sorry for all the questions, if only, these information were added in the question. Commented May 9 at 10:16
  • no on opposite thank you for your interest in my issue. yes I added "@Injectable({ providedIn: 'root', })" in the service. check the answer I just posted. I found the issue Commented May 9 at 10:52

1 Answer 1

0

I found the issue in case someone faces the same problem:

The issue was me injecting Firebase Messaging in the NotificationsService. It ran well on other platforms but for iOS there was a timing issue apparently. Using a lazy getter fixed the issue

code I changed to fix the issue

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.