Ahm*_*adi 5 notifications react-native expo
嗨,我是本地反应新手,我只是在 Android 上测试推送通知。但是当我通过 expo 客户端在我的 Android 设备上测试时没有振动或声音。
然而,当我通过 expo 客户端在 Android 设备上打开应用程序时,推送通知确实出现了,但没有声音,没有振动,即使我已经将它们设置为 true。
我想发出通知,而不是每天早上 8 点通知用户,即使应用程序已关闭,我可以做到吗?
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);
});
};
Run Code Online (Sandbox Code Playgroud)
我找到了声音和振动的解决方案,这不是最好的,正如我所说,我是 RN 新手,但它仍然有效(我使用了这个:“创建 Android 异步频道”)
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);
});
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4752 次 |
| 最近记录: |