Im trying to schedule a daily notification with expo notifications. I need to fire this notification from a specified date in the future. So it will be for example: schedule the notification on 20 days in the future and from there fire it daily until cancelled. How can't find anything in the documentation on how to do this. Does anyone have an idea?
You can set a starting date like this:
await Notifications.scheduleNotificationAsync({
content: {
title,
body,
sound: true,
vibrate: [0, 255, 255, 255],
},
trigger: {
date: "your-date"
},
});
Later on you can switch on repeats like this:
trigger: {
repeats: true //or false
},
I'm not sure what you mean by fire until cancelled. Should the user specifically cancel the notification or is som state variable changing? Either way, I think you can add repeats: true or false depending on any variable.
Hope this helps!
Related
I am currently using the react-native-push-notification library to schedule and receive notifications in my React Native app. I am able to cancel a scheduled notification using the cancelLocalNotification method, but this only cancels the notification for 24 hours. I want to find a way to remove a specific notification forever, so it will not be rescheduled.
I have tried using the following code to cancel a notification by its ID, but it only cancels it for 24 hours:
const onCancelNotification = async (id: string) => {
// Get a list of all scheduled notifications
PushNotification.getScheduledLocalNotifications((notifications) => {
// Iterate through the list of notifications
notifications.forEach((notification) => {
// Check if the notification is the one you want to cancel
if (notification.id.indexOf(id) === 0) {
// Cancel the notification
PushNotification.cancelLocalNotification(notification.id);
}
});
});
};
I would greatly appreciate any help or suggestions on how to achieve this.
This code snippet demonstrates a workaround for removing a specific scheduled local notification in React Native. The function onRemoveNotification takes in an id parameter, which is used to identify the specific notification that needs to be removed.
It's important to note that there is no direct method for removing a specific scheduled local notification in React Native. This code provides a workaround that can be used, but it's important to be aware that it relies on scheduling a notification with an earlier date and setting the repeatType to undefined.
Please note that this code is not a perfect solution and could have side effects on the app.
const onRemoveNotification = async (id: string) => {
// Get a list of all scheduled notifications
PushNotification.getScheduledLocalNotifications((notifications) => {
// Iterate through the list of notifications
notifications.forEach((notification) => {
// Check if the notification is the one you want to cancel
if (notification.data.notificationId?.indexOf(id) === 0) {
// Create a new date one day earlier than the current scheduled date
const earlyDate = moment(notification.date).add(-1, "day").toDate();
// remove the notification
// schedule the notification with an earlier date and repeat type undefined
// this will effectively "remove" the notification
// since it will not be displayed
PushNotification.localNotificationSchedule({
id: notification.id,
title: notification.title,
message: notification.message,
repeatType: undefined,
date: earlyDate,
});
// cancel the previous scheduled notification
PushNotification.cancelLocalNotification(notification.id);
}
});
});
};
I have a notification that users receive daily. It's supposed to be a reminder. However, users need to skip notifications ocasionally,
I am triggering these expo notifications using
Notifications.scheduleNotificationAsync({
content: {
title,
body,
data,
categoryIdentifier,
},
trigger: {
channelId,
hour: reminderTime.getHours(),
minute: reminderTime.getMinutes(),
repeats: true,
}
})
Is there a way to disable the notification temporarily say for a day? Right Now I am just deleting the channel and resetting it after that day. But I am assuming there is a better way?
maybe if you create an exception list and validate before sending the notification.
exception = "01/07/2022"
if (new Date() == new Date(exception)){
return console.log("[NOTIFICATIONS] - exception found")
}
// normal post
I am having a seriously tough time figuring out how to effectively manage events created with .createEventAsync() method in expo-calendar API. Docs found here. This is the structure of the parameter I am entering into .createEventAsync().
testEvent = {
calendarId: calendarId,
title: event.position,
startDate: shiftStart,
endDate: shiftEnd,
location: event.store.name,
alarms: [],
id: event.id,
}
The idea is to have the event id on the app calendar be the same as the id on the OS calendar, but it seems like that is not possible. I am trying to build a system that requests permissions, if permissions granted then sync all events on the app with the os calendar. If the event already exists, do not create another event.
The function runs a check with:
async function checkEventExists(eventId: any) {
try {
const event = await Calendar.getEventAsync(eventId)
console.log(console.log(event))
if (event) {
return true
}
} catch {
return false
}
}
If false, event is not created. If true then .createEventAsync() is called.
Currently, new instances of the same event are created every time the app polls data from the server and this is unrelated, but also creates 7 instances of 1 event each time data is polled. How do people usually manage and track calendar events after creation? I thought event id would be a good way to tell if the event is one and the same, but I was wrong.
I'm creating an app in react native using expo where I need to set both a custom start time and a custom repeat interval time for notifications based on user input. For example, I need to start notifications in 15 days and then repeat every 27 days thereafter. I can figure out how to get it to trigger in 15 days, or repeat every 27 days, but not both together. I need it to work on Android and iOS.
import * as Notifications from "expo-notifications";
export default function notificationsConfig(repeatLength) {
const config = {
content: {
title: "Reminder",
body: "body.",
sound: false,
priority: Notifications.AndroidNotificationPriority.HIGH,
color: "blue",
},
trigger: {
/****Is there a way to set first notification date here?******/
seconds: 60 * 60 * 24 * repeatLength,
repeats: true,
},
};
return config;
}
Thank you!
I'm using mauron85 Backgroundgeolocation plugin for my ionic 3 application.
It works, the only problem is the timing when it engage.
The problem is even if I subscribe and start the plugin, it engage only after a couple of minutes.
Namely it returns the position in the moment that I start it, but before engaging and actively tracking it takes some time.
Also sometime when I put the app in background it does not start recording.
That is the code I used:
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '#ionic-native/background-geolocation';
Then the consifugration and start:
let config: BackgroundGeolocationConfig = {
locationProvider: 0,
desiredAccuracy: 10,
stationaryRadius: 1,
distanceFilter: 1,
activitiesInterval: 100,
interval: 1000,
notificationTitle: 'TiCaccio is tracking you',
notificationText: 'you know it, right?'
// debug: true
};
this.backgroundGeolocation.configure(config).subscribe((location: BackgroundGeolocationResponse) => {
console.log("Background location", location);
this.lat = location.latitude;
this.lng = location.longitude;
});
this.backgroundGeolocation.start();
Now the problem is in fact that sometimes it engage, sometimes not. And even when it engage it just do that after a couple of minutes.