Skip to content

Commit 2cd490e

Browse files
authored
fix: android push notification permission issues (#2947)
1 parent 7ea43d4 commit 2cd490e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

examples/SampleApp/src/components/SecretMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ export const SecretMenu = ({ close, visible, chatClient }: { close: () => void,
8181
>
8282
Notification Provider
8383
</Text>
84-
<View style={{ marginLeft: 16 }}>
84+
{isAndroid ? null : <View style={{ marginLeft: 16 }}>
8585
{notificationConfigItems.map((item) => (
8686
<TouchableOpacity key={item.id} style={{ paddingTop: 8, flexDirection: 'row' }} onPress={() => storeProvider(item)}>
8787
<Text style={styles.notificationItem}>{item.label}</Text>
8888
{item.id === selectedProvider ? <Check height={16} pathFill={'green'} width={16} style={{ marginLeft: 12 }} /> : null}
8989
</TouchableOpacity>
9090
))}
91-
</View>
91+
</View>}
9292
</View>
9393
</View>
9494
<TouchableOpacity onPress={removeAllDevices} style={menuDrawerStyles.menuItem}>

examples/SampleApp/src/hooks/useChatClient.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { USER_TOKENS, USERS } from '../ChatUsers';
77
import AsyncStore from '../utils/AsyncStore';
88

99
import type { LoginConfig, StreamChatGenerics } from '../types';
10+
import { PermissionsAndroid, Platform } from 'react-native';
1011

1112
// Request Push Notification permission from device.
1213
const 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+
7081
export 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

Comments
 (0)