React native 在Expo客户端的Android推送通知中没有声音或振动
嗨,我是新来的本地人,我只是在安卓上测试推送通知。但当我通过expo客户端在android设备上测试时,并没有振动或声音 然而,当我通过expo客户端在我的android设备上打开应用程序时,推送通知确实会出现,但是没有声音,没有振动,即使我已经将这些设置为真 我想做一个通知,而不是每天早上8点通知用户,即使应用程序关闭,我能做吗React native 在Expo客户端的Android推送通知中没有声音或振动,react-native,notifications,expo,React Native,Notifications,Expo,嗨,我是新来的本地人,我只是在安卓上测试推送通知。但当我通过expo客户端在android设备上测试时,并没有振动或声音 然而,当我通过expo客户端在我的android设备上打开应用程序时,推送通知确实会出现,但是没有声音,没有振动,即使我已经将这些设置为真 我想做一个通知,而不是每天早上8点通知用户,即使应用程序关闭,我能做吗 async componentWillMount() { let result = await Permissions.getAsync(Permission
async componentWillMount() {
let result = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (result.status === "granted" && this.state.switchStatus) {
console.log("Notification permissions granted.");
this.setNotifications();
} else {
console.log("No Permission", Constants.lisDevice);
}
this.listenForNotifications();
}
getNotification(date) {
const localNotification = {
title: `Notification at ${date.toLocaleTimeString()}`,
body: "N'oubliez pas de prendre tes medicament",
ios: {
sound: true
},
android: {
sound: true,
priority: "max",
sticky: false,
vibrate: true
}
};
return localNotification;
}
setNotifications() {
Notifications.cancelAllScheduledNotificationsAsync();
for (let i = 0; i < 64; i++) {
//Maximum schedule notification is 64 on ios.
let t = new Date();
if (i === 0) {
t.setSeconds(t.getSeconds() + 1);
} else {
t.setMinutes(t.getMinutes() + 1 + i * 1);
}
const schedulingOptions = {
time: t
};
Notifications.scheduleLocalNotificationAsync(
this.getNotification(t),
schedulingOptions
);
}
}
listenForNotifications = () => {
Notifications.addListener(notification => {
console.log("received notification", notification);
});
};
异步组件willmount(){
让结果=wait Permissions.getAsync(Permissions.NOTIFICATIONS);
if(result.status==“已授予”&&this.state.switchStatus){
log(“已授予通知权限”);
这个.setNotifications();
}否则{
console.log(“无权限”,Constants.lisDevice);
}
this.listenForNotifications();
}
获取通知(日期){
const localNotification={
标题:`Notification at${date.toLocaleTimeString()}`,
正文:“N'oubliez pas de prendre tes medical”,
ios:{
听起来:没错
},
安卓:{
听起来:是的,
优先级:“最大”,
sticky:错,
振动:正确
}
};
返回本地通知;
}
setNotifications(){
通知。CancelAllScheduledNotificationAsync();
for(设i=0;i<64;i++){
//ios上的最大日程通知数为64。
设t=新日期();
如果(i==0){
t、 设置秒(t.getSeconds()+1);
}否则{
t、 setMinutes(t.getMinutes()+1+i*1);
}
常数计划选项={
时间:t
};
Notifications.scheduleLocalNotificationAsync(
此.getNotification(t),
计划选项
);
}
}
ListInfo通知=()=>{
Notifications.addListener(通知=>{
日志(“收到通知”,通知);
});
};
我找到了一个声音和振动的解决方案,这不是最好的,因为我说我是RN的新手,但它仍然有效(我用了这个:“创建Android Async频道”) 异步组件willmount(){ 让结果=wait Permissions.getAsync(Permissions.NOTIFICATIONS); if(result.status==“已授予”&&this.state.switchStatus){ //我补充说: 如果(Platform.OS==='android'){ Notifications.createChannelAndroidAsync('chat-messages'{ 名称:'聊天信息', 听起来:是的, 振动:是的, }); } log(“已授予通知权限”); 这个.setNotifications(); }否则{ console.log(“无权限”,Constants.lisDevice); } this.listenForNotifications(); } 获取通知(日期){ const localNotification={ 标题:`Notification at${date.toLocaleTimeString()}`, 正文:“N'oubliez pas de prendre tes medical”, ios:{ 听起来:没错 }, 安卓:{ “channelId”:“聊天信息”//以及 } }; 返回本地通知; } setNotifications(){ 通知。CancelAllScheduledNotificationAsync(); for(设i=0;i<64;i++){ //ios上的最大日程通知数为64。 设t=新日期(); 如果(i==0){ t、 设置秒(t.getSeconds()+1); }否则{ t、 setMinutes(t.getMinutes()+1+i*1);//15分钟 } 常数计划选项={ 时间:t }; Notifications.scheduleLocalNotificationAsync( 此.getNotification(t), 计划选项 ); } } ListInfo通知=()=>{ Notifications.addListener(通知=>{ 日志(“收到通知”,通知); }); };
在我的例子中,我允许在(设置->通知->你的应用程序名->声音->允许)中使用通知声音,并且在你的app.json文件中添加
android:{useNextNotificationApi:true}
非常感谢!你的代码对我来说很好,但是只有前一两个通知通过,然后就停止了。知道为什么吗?谢谢!该代码只是用于解决不接收通知的问题。因此,现在您必须根据需要安排通知,并且它将起作用。很抱歉回复太晚^^欢迎光临。你能不能再多加一点关于这有什么帮助的上下文和一些参考信息。这有助于将来的搜索了解目的解决方案的价值。
async componentWillMount() {
let result = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (result.status === "granted" && this.state.switchStatus) {
// i add this :
if (Platform.OS === 'android') {
Notifications.createChannelAndroidAsync('chat-messages', {
name: 'Chat messages',
sound: true,
vibrate: true,
});
}
console.log("Notification permissions granted.");
this.setNotifications();
} else {
console.log("No Permission", Constants.lisDevice);
}
this.listenForNotifications();
}
getNotification(date) {
const localNotification = {
title: `Notification at ${date.toLocaleTimeString()}`,
body: "N'oubliez pas de prendre tes medicament",
ios: {
sound: true
},
android: {
"channelId": "chat-messages" //and this
}
};
return localNotification;
}
setNotifications() {
Notifications.cancelAllScheduledNotificationsAsync();
for (let i = 0; i < 64; i++) {
//Maximum schedule notification is 64 on ios.
let t = new Date();
if (i === 0) {
t.setSeconds(t.getSeconds() + 1);
} else {
t.setMinutes(t.getMinutes() + 1 + i * 1); // 15 Minutes
}
const schedulingOptions = {
time: t
};
Notifications.scheduleLocalNotificationAsync(
this.getNotification(t),
schedulingOptions
);
}
}
listenForNotifications = () => {
Notifications.addListener(notification => {
console.log("received notification", notification);
});
};