我需要通知来反复振动手机,直到用户执行单击通知等操作。我还没有找到办法让这项工作发挥作用。起初,我试图使用 React Native Expo 的 Vibrate Api 来解决这个问题。但我后来意识到,如果应用程序被终止并且用户没有与通知交互如此处所述,则博览会无法对通知做出反应。唯一的其他方法是为通知设置振动和间隔。但我似乎找不到办法做到这一点。我正在使用 Node js 发送通知,这实际上与 this 相同的代码。这是唯一用于发送消息的格式。没有振动。但在初始页面上,我看到他们添加了振动。我一定是在某个地方弄错了或者迷路了。我想这可以通过某种方式完成,否则就不会有警报应用程序。我怎样才能做到这一点?在我的 Android 手机上,通知甚至根本不会振动,如果我没有此功能,该应用程序将毫无用处。谢谢):
我后来找到了这个问题的答案。在 andriod 上,您必须创建一个 channel 并在发送通知时包含它。文档中的所有解释。您将能够创建振动模式等等。另外,如果你像我一样在后端发送通知(我使用了nodejs),你仍然可以创建一个通道,但它必须在前端。在 useEffect 中使用此代码:
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
您可以将“默认”替换为您想要调用通知通道的任何名称。我还没有找到如何在 ios 上执行此操作。如果我这样做,我会更新。希望我不会忘记):
您也可以帮助我吗,我有这段代码,我希望手机在收到通知时振动并播放声音,直到用户通过按下即将到来的按钮与通知进行交互。目前还没有响,下面是我的代码
useEffect(()=> {
// Add a notification response listener
const notificationResponseListener = Notifications.addNotificationResponseReceivedListener(
handleNotificationResponse
);
const backgroundSubscription = Notifications.addNotificationReceivedListener(
handleBackgroundNotification
);
Notifications.setNotificationCategoryAsync('acknowledge', [
{
buttonTitle: "Coming",
identifier: "reply",
options: { opensAppToForeground: true },
}
]);
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('alert', {
name: 'alert',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return () =>{
Notifications.removeNotificationSubscription(notificationResponseListener);
Notifications.removeNotificationSubscription(backgroundSubscription);
}
}, [])
// Define a function to handle notification interaction response
const handleNotificationResponse = async (response) => {
console.log(response)
//Vibration.cancel();
await Notifications.dismissAllNotificationsAsync()
};
const handleBackgroundNotification = async (notification) => {
// Play notification sound
// Replace with your sound playing logic
// Example:
// SoundPlayer.playSoundFile('notificationSound.mp3', 'mp3');
// Vibrating continuously
//Vibration.vibrate([0, 250, 250, 250], true);
};
useEffect(() => {
socket.emit('user_login', user?.id);
socket.on('driver_arrived', async (param) => {
console.log('driver arrived')
await Notifications.scheduleNotificationAsync({
content: {
title: param.title,
body: param.message,
categoryIdentifier: 'acknowledge',
sound: "default",
vibrate: [0, 250, 0, 250],
},
trigger: null
});
});
if (stores.length !== 0) {
socket.on('store_online', (store_id) => {
const index = stores.findIndex(store => store.id === store_id);
if (index !== -1) {
const updatedStores = [...stores];
updatedStores[index] = { ...updatedStores[index], status: true };
setStores(updatedStores);
}
});
socket.on('store_offline', (store_id) => {
const index = stores.findIndex(store => store.id === store_id);
if (index !== -1) {
const updatedStores = [...stores];
updatedStores[index] = { ...updatedStores[index], status: false };
setStores(updatedStores);
}
});
}
socket.on('orderResponse', (order)=> {
if (order.status === 'accepted') {
setCart([])
navigation.navigate('TrackingScreen', {order: order, back: false})
} else if (order.status === 'rejected') {
Alert.alert(
'Rejected',
'The restaurant rejected your order, please try another meal or restaurant',
[
{
text: 'Ok',
onPress: () => navigation.replace('HomeScreen'),
style: 'cancel',
},
],
);
}else{
navigation.goBack()
}
clearTimeout(timeoutId);
setTimeoutId(null);
});
return () => {
socket.off();
};
}, [stores, user]);
useEffect(() => {
const setupNotifications = async () => {
try {
const { status } = await Notifications.getPermissionsAsync();
if (status !== 'granted') {
await Notifications.requestPermissionsAsync()
} else {
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("alert", {
name: "alert",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
const pushToken = await Notifications.getExpoPushTokenAsync({
projectId: Constants.expoConfig.extra.eas.projectId,
});
setExpoPushToken(pushToken.data)
if (user?.pushToken === '' || user?.pushToken === null) {
const token = await AsyncStorage.getItem('@token')
await axios.get(`${apiUrl}/add-token/${pushToken.data}`,{
headers: {
Authorization: `Bearer ${JSON.parse(token)}`
}
})
}
}
} catch (error) {
console.log('Push Notification Permissions error:', error);
}
};
setupNotifications();
}, []);