I want to retrieve the next time the notification is triggered - ios

I use the in local notation repeatInterval, Is it possible to retrieve what time the repeatInterval will work again? I need this to update the counter "how much is left before triggering the notification".

If you're targeting iOS 10+ you should use UNUserNotificationCenter.
UNUserNotificationCenter has method getPendingNotificationRequests(completionHandler:) that will return in completion handler list of pending notification requests.
Each element of the list is UNNotificationRequest object, which contain property trigger.
If the trigger is UNTimeIntervalNotificationTrigger or UNCalendarNotificationTrigger it has method nextTriggerDate() which will give you the timepoint when it should be triggered next time.
And it looks like what are you looking for.

Related

Is it good practice to pass a specific NSTimeInterval to setMinimumBackgroundFetchInterval?

I'm I obliged to pass either UIApplicationBackgroundFetchIntervalMinimum or UIApplicationBackgroundFetchIntervalNever ?
Or Is it possible to pass a custom interval instead, for example 10 minutes:
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(600.0)
In my App, I have to download some new updated data while the app is in the background.
Then I'll use the new data to send a local notification to the user.
I looked up apis and found this:
func setMinimumBackgroundFetchInterval(_ minimumBackgroundFetchInterval: NSTimeInterval)
And there is a tip for this:
The minimum number of seconds that must elapse before another
background fetch can be initiated. This value is advisory only and
does not indicate the exact amount of time expected between fetch
operations.
So I think you'd better not depend on that time interval.

Fast way of finding scheduled notifications

Is there a fast and efficient way of finding scheduled notifications? Find list of Local Notification the app has already set discusses going through the list, which is what I did:
// To avoid duplicate notifications, check whether there are already scehduled notifications with the same fireDate, reminderName and calendar name.
if let definiteDueDateComponents = reminder.dueDateComponents, definiteDueDate = definiteDueDateComponents.date, definiteScheduledNotifications = UIApplication.sharedApplication().scheduledLocalNotifications {
for notification in definiteScheduledNotifications {
if notification.fireDate?.compare(definiteDueDate) == .OrderedSame {
// Check whether there is already a notification set up for definiteDueDate?
// If so, check whether the notification is actually for the same item that I want to set up here.
}
}
}
This, however, may become inefficient, especially if I have many items that I want to check against (run the above code) before I schedule them and also if I already have various scheduled notifications.
Has anybody experimented with creating a dictionary (hash table) of scheduled notifications? If so, when do you create it and recreate it? Is it cumbersome trying to keep the hash table in synch with UIApplication.sharedApplication().scheduledLocalNotifications?
Even if you have 10000 scheduled notifications you would probably not be able to detect the amount of time it took to iterate through them with your eyes. You'd have to log the time before and after and calculate the difference, and it would likely be less than 1/1000 of a second.
This is a case of "premature optimization". Use a for loop and be done with it.
If you have more than 10000 scheduled notifications then you need to rethink your design.

UIApplication.sharedApplication.scheduledLocalNotifications is always empty

Even if I add a new local notification right before, the attribute is empty. I found a lot of post (and just one on stack overflow) - but nobody has solved this problem.
My useless is, that I want to delete a local notification. That's why I want to iterate over the array and compare the hash value of my notification to delete and the current iterator object.
The notification fires correctly.
Add notification to the array
UIApplication.sharedApplication().scheduleLocalNotification(newNotification)
Read the array
for notification in application.scheduledLocalNotifications {
if notification.hashValue == hashValue {
application.cancelLocalNotification(notification as! UILocalNotification)
NSLog("Unsheduled local notification for \(notification.alertBody!)")
}
}
Thanks for your help.
Seems I have not been first to be stucked on it..
But seems answer is much closer than I tought.
Cmd + LPM
public var scheduledLocalNotifications: [UILocalNotification]? // setter added in iOS 4.2
The property is just form of setter. And probably was never intended to get scheduled notifications
It seems that checking the UIApplication's scheduledLocalNotifications array is fairly unreliable.
Most people seem to recommend just keeping your own list as well, and querying that.
Firstly, that array will only contain notifications that are scheduled after the current date, so any that have been registered that are in the past or any that have already fired, will not be added.
For me, that was not the problem, the notificatiosn I was registering were definitely in the future, but still didn't appear in the list. The best answer I could find is:
Having similar issues right now. My guess here is that iOS does not schedule the notifications immediately but only at the end of the current run loop. I am running into these problems when setting the scheduledLocalNotifications property several times in the same run loop and changes don't seem to be updated accordingly. I think I will just keep a copy of the local notifications array myself and only set scheduledLocalNotifications and never read it.
(source)

How to get notified when some proprerty of PFObject changed?

Im using parse.com and I wonder if there is a possible approach to get notified when PFObject on parse change one of his properties. The only sollution that I see is to use Push notifications and when notification arrive a refresh the object. Is this the bes possible way or there is a better one?
Thanks.
I don't think there is a way to do this. As you suggested I would simply refresh the PFObject before using it
You could create cloud-code .beforeSave / .afterSave function, which will trigger notification to be send. However it could be very wasteful, so perhaps depending on your timing needs you may also query for objects with recent updatedAt dates every once in a while ?
Parse.Cloud.beforeSave("className", function(request, response) {
if (request.object.existed() ) {
//this is update of already existing object
//... check if change has been done and trigger new notification
}
});

iOS Different Actions for Different Local Notifications?

My app has two different features that can each schedule a local notification. They are both reminders, but for different things, and for different parts of the app. Is there a way to schedule 2 different actions for these? For example, clicking notification style 1 sends you to the first tab, clicking notification style 2 sends you to the 2nd tab?
Yes this is possible, just add a custom NSDictionary to the UILocalNotification userinfo property.
For example, add a type when you create the UILocalNotification:
myLocalnotification.userInfo = #{#"type" : #"openTab1"};
Then in the application:didReceiveLocalNotification: you could do:
if ([notification.userInfo[#"type"] isEqualToString:#"openTab1"]) {
// Your code to open tab1
}
UILocalNotifications supports the userInfo dictionary. You can add some information here to trigger the response you want. For instance you can create you own "action" dictionary:
#{ #"action" : #"open_tab_1"}
When you receive the notification you inspect the userInfo and check the action key and trigger the correct behavior by just checking the equality of 2 strings.

Resources