I want to check whether user has disabled push notification permission on device settings for both iOS and Android, every time during app launch and resume from background. If user has disallowed the notifications permission, I will prompt an alert message to user. I have used One Signal SDK to implement this feature.

iOS

  • I use function addPermissionObserver to check user accepting or declining the permission prompt
  • Subsequently I use function getPermissionSubscriptionState to check user has allowed or disallowed the notification permission on device settings.

Android

  • I use function getPermissionSubscriptionState to check user has allowed or disallowed the notification permission on device settings.

I have tested for Android, it works perfectly. However I encountered issue for iOS on displaying pop up alert to user if has disallowed notifications permission.

Reproduce steps as below -

Test Case 1

  1. Accept permission prompt
  2. Put app in background
  3. Go to device setting and turn off notifications permission
  4. Resume app from background
  5. No popup alert displayed
  6. Put app in background
  7. Resume app from background
  8. Popup alert only display

Test Case 2

  1. Decline permission prompt
  2. Put app in background
  3. Go to device setting and turn on notifications permission
  4. Resume app from background
  5. Popup alert still display
  6. Put app in background
  7. Resume app from background
  8. Popup alert no more display

My Code as below, I have implemented the functions in App.Component.ts

export class AppComponent {
    firstLaunch = false;


    constructor(
        private platform: Platform,
        private alertController: AlertController,
        private storage: Storage
        ) {
        this.initializeApp();
    }

    initializeApp() {
        this.platform.ready().then(() => {
            this.checkNotificationPermission();
        });

        this.platform.resume.subscribe((result) => {
            this.checkNotificationPermissionState();
        });
    }

    checkNotificationPermission() {
        this.storage.get('firstLaunch').then(value => {
            this.firstLaunch = value;

            if (this.firstLaunch) {
                this.checkNotificationPermissionState();
            } else {
                //First time launch and update the flag
                this.storage.set('firstLaunch', true).then(() => {
                    if (this.platform.is('ios')) {
                        //User accept or decline the permission prompt
                        this.oneSignal.addPermissionObserver().subscribe(async data => {
                            if (data.to.status == 1) {
                                const alert = await this.alertController.create({
                                    header: 'Test',
                                    mode: 'ios',
                                    message: 'You have disallowed',
                                    buttons: ['Ok']
                                });
                                alert.present();
                            }
                        });
                    }
                });
            }
        });
    }

    checkNotificationPermissionState() {
        this.oneSignal.getPermissionSubscriptionState().then(async status => {
            //iOS only: Integer: 0 = Not Determined, 1 = Denied, 2 = Authorized
            //Android only: Integer: 1 = Authorized, 2 = Denied
            if (status.permissionStatus.state == 2 || status.permissionStatus.status == 1) {
                const alert = await this.alertController.create({
                    header: 'Test',
                    mode: 'ios',
                    message: 'You have disallowed',
                    buttons: ['Ok']
                });
                alert.present();
            }
        }).catch(respError => {
        });
    }
}
0

There are 0 best solutions below