I have a time notification in my swift app that sends a push notification locally everyday at 8. I want to add a button so that if it is pressed, it cancels the push notifications for that day only. My question is does removeAllPendingNotificationRequests() do the work or will it cancels time notifications forever?
It cancels all the pending notifications that you've registered. In your scenario you could store the idenfifiers of the Notification that you're registering in a dictionary where the key corresponds to each day which is of type Date and the value is an array of identifiers which is of type [String]. And if you want to cancel all the Notification for a particular Date you can get the array of String identifiers for that date from the above dictionary and cancel them like the following, here's an example:
let notificationsDictionary = [Data: [String]]()
guard let notificationsForToday = notificationDictionary[Data()] else { return }
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: notificationsForToday)
Related
What if I want to make a page resets itself at 12:00 am or after specific time.
Let's say I want the user to use a specific functionality once every day. For example, if the user click on a button will sees a pic of a dag, and after 12:00 am the user can click on the button again to see another picture of another dog in a specific list of dogs' pics stored in the app. How this is can be implemented in Flutter?
Definitely there are different ways to do it. Assuming you'd like to handle the logic in the client side, you can store a local boolean value called 'seen' in the SharedPreferences.
Then somewhere in your app (startup for example), you can update the value to false if the time is after midnight. Assuming you already have SharedPreferences setup and it's in a variable called sharedPreferences
DateTime time = DateTime.now();
String resetTime = "${time.year}-${time.month.toString().padLeft(2,'0')}-${time.day.toString().padLeft(2, '0')} 00:00:00";
if(time.isAfter(DateTime.parse(resetTiem){
sharedPreferences.setBool('seen', false)
}
Now on the other side of the code, where the user clicks to see the picture, you can check if the sharedPreferences value is true or false in order to show the picture.
if(sharedPreferences.getBool('seen')){
return SomeWidgetWithThePicture;
} else {
return AnotherWidgetWithoutPicture // tell the user they can see it after midnight
}
I have a variable that keeps track of user statistic I want to reset at the beginning of each day. How can I do that?
Since the application is not allowed to run in the background, it seems I will have to do the check every time the application is active but I don't know how to reset the variable I have only once. This is the function I wanted to use:
let beginingOfDay = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
func resetCurrentTime(){
// Date comparision to compare current date and begining of the day.
let dateComparisionResult:NSComparisonResult = NSDate().compare(beginingOfDay)
if dateComparisionResult == NSComparisonResult.OrderedDescending || dateComparisionResult == NSComparisonResult.OrderedSame {
// Current date is greater or equal to end date.
currentTime = 0 //reset the time tracker
}
}
I wanted to use this function to check when the application is launched but the problem is that the application could be launched many time a day. How I can reset my variable only once at the beginning of a day if the user is using the application or when the application becomes active or is launched for the first time that day?
Thanks
You can store in the user defaults this value.
So the flow is the following:
When the app is launched or became active you check whether the value of the variable in the user defaults is the same as the current day (e.g. 25/07/2016), then do nothing.
If the value is different, then you update the value in the user defaults with the current day.
If the app is running and the date is changed, you can update the value of your variable by subscribing to this notification:
UIApplicationSignificantTimeChangeNotification
I need to update the messenger's status when there is a calender event is happening in Thunderbird. Is it possible to hook the existing alarm?
Do you want to update the status any time there is an event, or when an alarm fires? Unfortunately for both options there is no built-in way. You would have to create an extension that listens to the respective events and then connects with your messenger.
Any time there is an event
Ideally there would be an observer service notification when an event is in progress and when it ends, but internally we didn't come across a situation where we needed this yet. Its a very nice feature request, so if you'd like to add this feature to core and then use if from your extension please let me know.
Anyway, one way to handle this would be to run a timer every 15 minutes or so that retrieves items from all enabled calendars for the current time. When the timer fires, you can request events at the current time from all calendars. To do so, you should:
// Create a composite calendar
var composite = Components.classes["#mozilla.org/calendar/calendar;1?type=composite"]
.createInstance(Components.interfaces.calICompositeCalendar);
// Add all current calendars into the composite
var calendars = cal.getCalendarManager().getCalendars({});
for (let calendar of calendars) {
if (!calendar.getProperty("disabled")) {
composite.addCalendar(calendar);
}
}
// In newer versions of Lightning you can use calAsyncUtils.jsm like this:
var pcal = cal.async.promisifyCalendar(composite);
var now = cal.now();
pcal.getItems(Components.interfaces.calICalendar.ITEM_FILTER_ALL_ITEMS, 0, now, now).then(function(items) {
if (items.length) {
// Something is going on right now
} else {
// Nothing is going on
}
});
If you want to improve on this, you could at startup get the next event occurring and set the timer interval accordingly. This way you don't have empty timer runs and a more exact result.
When an alarm fires
This is far simpler. You can listen to the alarm service to determine when an alarm fires and act upon it.
cal.getAlarmService().addObserver({
onAlarm: function(aItem, aAlarm) {
// Alarm fired, update your messenger
},
onRemoveAlarmsByItem: function(item) {},
onRemoveAlarmsByCalendar: function(calendar) {},
onAlarmsLoaded: function() {}
});
This question already has an answer here:
For loop and if statement for multiple conditions
(1 answer)
Closed 6 years ago.
I've got a Google Form set up to record user-reported errors about a database we maintain. The responses go into a Google Sheet and the users email is recorded. Essentially, I'd like to have a status field in that Google Sheet -- and when it's set to something like "Complete" (which would be in the same row as the response) I would like an email to be automatically sent to the user that submitted the response, letting them know the status of their response is complete. So sort of like a ticket system that many companies use (but we don't have a lot of bandwidth to set this up, so we're looking for something simple/free).
You cannot send an email inside the onEdit trigger. So you'll have to save the edits somewhere, maybe inside UserProperties, and have a time-based trigger that sends this value to your email every minute.
See: Email Notifications in Google Spreadsheets.
Google Spreadsheet support email notifications for row edits (tools - notification rules) but the last time I tried it, it never worked.
Create an onEdit() function, capture the cells value when it's edited, and run the code.
function onEdit(e){
// Capture the cells value after it is edited
var valCell = e.value;
Logger.log('The cell value is: ' + valCell);
if (valCell === "Complete") {
//Get the range of the cell
var theRange = e.range;
//Get the row of the range
var theRowOfEdit = theRange.getRow();
// Returns the cell with email
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B" + theRowOfEdit.toString());
//Get the user email
var userEmail = cell.getValue();
//Send the email
}
}
This is not the complete code that you need. But, set this code up; test it, debug it, and if you have a specific question, post another question with the error message and line of code that isn't working.
Use debug in the code editor, and/or Logger.log() statements to debug the code.
My application generates multiple local notifications and while I'm testing it, I see that the app continuously receives the local notification appDelegate message. This is in spite of my having no repeat interval.
<UIConcreteLocalNotification: 0x1574b0>{fire date = (null), time zone = (null), repeat interval = 0, next fire date = 2011-09-23 19:40:35 +0000}
This is the debug value returned at my breakpoint. As you'll notice, there is no data except the next fire date, which shouldn't technically be called because there is no interval. Also, this is the only notification in the SharedApplication LocalNotifications array.