Local Schedule Notification Throwing Multiple Notifications at Once Instead of Single on Android
See original GitHub issue🐛 Bug Report
Environment
I am trying to create water reminder app with notification feature once in every hour. I have implemented notifications with scheduleLocalNotificationAsync. My implementation works as expected in IOS but in Android when I open the app, the app sending 12 notifications at the same time instead of sending them one by one at scheduled time.
Reproducible Demo
My code is below, I didnt create snack because testing notifications is quite hard. Maybe you can understand from my code what is wrong with it. If you still need a snack, I can create it.
componentDidMount = () => {
this._sendNotifications();
};
_sendNotifications = async () => {
// Not to create duplicated notifications first cancel all notifications
await Notifications.cancelAllScheduledNotificationsAsync();
// beginning of notification part
const localnotification = {
title: 'Water Reminder',
body: "Don't forget to drink water!",
android: {
sound: true,
},
ios: {
sound: true,
},
};
// get the current date
let currentDate = Date.now();
currentDate = new Date(currentDate);
// get the day, month and year from current date to create time to schedule
let year = currentDate.getFullYear();
let month = currentDate.getMonth();
let date = currentDate.getDate();
// then create unix epoch number for eact date with number from notification section
// then we call notification function with each timestamp (not1)
if (this.state.switchStatus === false) {
await Notifications.cancelAllScheduledNotificationsAsync();
} else {
// Notification for nine
if (this.state.otherSwitchStatus.nine === true) {
let not0 = new Date(year, month, date, 9);
not0 = Date.parse(not0);
const schedulingOptions0 = { time: not0, repeat: 'day' };
// call the function to send notification at 9:00
await Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions0);
}
// Notification for ten
if (this.state.otherSwitchStatus.ten === true) {
let not1 = new Date(year, month, date, 10);
not1 = Date.parse(not1);
const schedulingOptions1 = { time: not1, repeat: 'day' };
// call the function to send notification at 10:00
await Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions1);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
React Native - Expo - Local Schedule Notification Throwing ...
I have implemented notifications with scheduleLocalNotificationAsync. My implementation works as expected in IOS but in Android when I open the ...
Read more >[Solved]-React Native - Expo - Local Schedule Notification ...
Coding example for the question React Native - Expo - Local Schedule Notification Throwing Multiple Notifications at Once Instead of Single on Android-React ......
Read more >Create a Notification
If your app posts multiple notifications in one second, they all appear as expected, but only the first notification per second makes a...
Read more >Local notifications in Xamarin.Forms
By default, notifications scheduled using the AlarmManager class will not survive device restart. However, you can design your application to ...
Read more >Push Notification Best Practices: 35 Tips for Dramatically ...
One thing to remember: while they may look alike, there is a difference between in-app notifications and push notifications.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Szymon20000 I guess I understand what is going on with the code.
Since as you can see in my code, independent of current time of the day, I am calling scheduleLocalNotificationAsync for 15 times (starting from 09:00 to 23:00, with daily repeat option). However when I open Notification screen at 12:40 I receive 4 notifications immediately (for one each 09:00, 10:00, 11:00 and 12:00).
I guess expo immediately calling notification function for 4 passed times. This is not the case for ios but it is happening in Android. I guess it needs to be fixed in expo side.
Until then I solved the issue by checking current hour and compare it with time to be scheduled and if it is passed, I schedule it for tomorrow.
@sinansonmez Are your notifications working when your app is closed ? My notifications work only on foreground and background, not when the app is closed.
Am I missing something ?