diff --git a/native/push/push-handler.react.js b/native/push/push-handler.react.js --- a/native/push/push-handler.react.js +++ b/native/push/push-handler.react.js @@ -135,7 +135,7 @@ ), commIOSNotificationsEventEmitter.addListener( 'remoteNotificationsRegistrationFailed', - this.failedToRegisterPushPermissions, + this.failedToRegisterPushPermissionsIOS, ), commIOSNotificationsEventEmitter.addListener( 'notificationReceivedForeground', @@ -323,9 +323,18 @@ } async ensureAndroidPushNotifsEnabled() { - const hasPermission = await CommAndroidNotifications.hasPermission(); + let hasPermission = await CommAndroidNotifications.hasPermission(); + const canRequestPermission = + await CommAndroidNotifications.canRequestNotificationsPermissionFromUser(); + + if (canRequestPermission) { + const permissionResponse = + await CommAndroidNotifications.requestNotificationsPermission(); + hasPermission = hasPermission || permissionResponse; + } + if (!hasPermission) { - this.failedToRegisterPushPermissions(); + this.failedToRegisterPushPermissionsAndroid(!canRequestPermission); return; } @@ -333,7 +342,7 @@ const fcmToken = await CommAndroidNotifications.getToken(); await this.handleAndroidDeviceToken(fcmToken); } catch (e) { - this.failedToRegisterPushPermissions(); + this.failedToRegisterPushPermissionsAndroid(!canRequestPermission); } } @@ -374,15 +383,22 @@ ); } - failedToRegisterPushPermissions = () => { + failedToRegisterPushPermissionsIOS = () => { this.setDeviceToken(null); if (!this.props.loggedIn) { return; } - const deviceType = Platform.OS; - if (deviceType === 'ios') { - iosPushPermissionResponseReceived(); - } else { + iosPushPermissionResponseReceived(); + }; + + failedToRegisterPushPermissionsAndroid = ( + shouldShowAlertOnAndroid: boolean, + ) => { + this.setDeviceToken(null); + if (!this.props.loggedIn) { + return; + } + if (shouldShowAlertOnAndroid) { this.showNotifAlertOnAndroid(); } };