@@ -7,6 +7,7 @@ import { USER_TOKENS, USERS } from '../ChatUsers';
77import AsyncStore from '../utils/AsyncStore' ;
88
99import type { LoginConfig , StreamChatGenerics } from '../types' ;
10+ import { PermissionsAndroid , Platform } from 'react-native' ;
1011
1112// Request Push Notification permission from device.
1213const requestNotificationPermission = async ( ) => {
@@ -67,6 +68,16 @@ messaging().setBackgroundMessageHandler(async (remoteMessage) => {
6768 }
6869} ) ;
6970
71+ const requestAndroidPermission = async ( ) => {
72+ if ( Platform . OS === 'android' && Platform . Version >= 33 ) {
73+ const result = await PermissionsAndroid . request (
74+ PermissionsAndroid . PERMISSIONS . POST_NOTIFICATIONS
75+ ) ;
76+ return result === PermissionsAndroid . RESULTS . GRANTED ;
77+ }
78+ return true ;
79+ } ;
80+
7081export const useChatClient = ( ) => {
7182 const [ chatClient , setChatClient ] = useState < StreamChat < StreamChatGenerics > | null > ( null ) ;
7283 const [ isConnecting , setIsConnecting ] = useState ( true ) ;
@@ -98,10 +109,14 @@ export const useChatClient = () => {
98109 await AsyncStore . setItem ( '@stream-rn-sampleapp-login-config' , config ) ;
99110
100111 const permissionAuthStatus = await messaging ( ) . hasPermission ( ) ;
101- const isEnabled =
112+ let isEnabled =
102113 permissionAuthStatus === messaging . AuthorizationStatus . AUTHORIZED ||
103114 permissionAuthStatus === messaging . AuthorizationStatus . PROVISIONAL ;
104115
116+ if ( permissionAuthStatus === messaging . AuthorizationStatus . DENIED ) {
117+ isEnabled = await requestAndroidPermission ( ) ;
118+ }
119+
105120 if ( isEnabled ) {
106121 // Register FCM token with stream chat server.
107122 const firebaseToken = await messaging ( ) . getToken ( ) ;
0 commit comments