"expo": "^39.0.0"
/ Library - expo-notifications.
Since moving to the new API, I have been facing various problems. I fixed problems with the icon, with receiving a notification in apk, with handlers, and now I could not find an answer to this question.
I have 3 phones Samsung Galaxy S9+ with Android 10
, Xiaomi Redmi 4x with Android 7.12 N2G47H MIUI 10 Global 9.6.27
and Xiaomi Redmi Note 4 Global Android 7.0 nrd90m MIUI global 11.0.2
.
Samsung Galaxy S9+. With my current config i receive notification excellent as it should be:
All this come from the box, i do not need to ask permissions or something like that.
Xiaomi Redmi 4x and Xiaomi Redmi Note 4. With my current config i have problems:
AND THE MAIN PROBLEM: All this DO NOT come from the box, I need to manually give permission for notifications to pop up, sound, etc.
My app.json :
{
"expo": {
"name": "qwert",
"slug": "qwert",
"version": "1.1.2",
"orientation": "portrait",
"icon": "./src/assets/images/logo1024.png",
"scheme": "myapp",
"userInterfaceStyle": "automatic",
"privacy": "unlisted",
"splash": {
"image": "./src/assets/images/splashScreen.png",
"resizeMode": "contain",
"backgroundColor": "#f1f0f0"
},
"android": {
"package": "com.qwert.app",
"googleServicesFile": "./google-services.json",
"versionCode": 1,
"useNextNotificationsApi": true
},
"notification": {
"icon": "./src/assets/images/VIAicon96.png",
"color": "#8edcd5",
"androidMode": "collapse"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"web": {
"favicon": "./assets/images/favicon.png"
},
"description": ""
}
}
My permissions/configs:
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import { Platform } from 'react-native';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
export const registerForPushNotificationsAsync = async () => {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS,
);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {
console.log('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
console.log('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
// eslint-disable-next-line consistent-return
return token;
};
Also, place where use all this:
useEffect(() => {
const handleNotification = async () => {
await getPermitsAndIntegrateIntoApp(store); // works great
};
const setPersistDataToStore = async () => {
const EXPO_PUSH_TOKEN = await registerForPushNotificationsAsync();
const qwertyyy = await getConfig(Constants.deviceId, EXPO_PUSH_TOKEN);
store.setQwerrt(config.destinations);
};
setPersistDataToStore();
Notifications.addNotificationReceivedListener(handleNotification);
return () => Notifications.removeAllNotificationListeners();
}, [store]);
On all 3 phones I have a logic for handling notifications ( send request, updates store, and updates screen on my app). It works correct.
How to automatically configure the permission to show push notifications? How to enable vibration on Xiaomi?
Finally i found an answers.
https://github.com/expo/expo/issues/8556 tells us that
And that
Permissions.askAsync not working as expected tells us how to let user set permissions to notifications.
And i still do not know why Xiaomi do not vibrate......