I'm working on a prayers application that enables users to set an alarm(local notification) for prayer times, i.e. the user sets the application to notify him for Fajr prayer every day, the problem is that the time for each prayer changes daily so the time the app will notify the user for fair in thursday will differ from the time in friday, i need to repeat the local notification every day but according to the daily prayer time, please, could anyone give me an idea?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
So the problem appears you need to be setting this local notification every now and then, but can't be a repeatable notification. I assume the user sets the prayer time, and wants to be notified. I suggest you set a few of them, since you know from the list. Then set background fetch for let say every 5 hours, and upon app background launch, just check what local notifications are still set, and update the list accordingly based on the current date. Background fetch doesn't wake your app precisely every 5 hours in this case, but will do its best. I'm sure your app will wake up at least twice a day. You can tweak the time based on your needs.
For more information refers to Apple's documentation on background execution:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
There are a few possible solutions for this. It might be safer to use an approach where a limited number of notifications are scheduled at a time, since iOS only keeps the 64 soonest notifications:
Source: UILocalNotification class reference
It is also not a good idea to rely on using the
UILocalNotification
passed intoapplication:didFinishLaunchingWithOptions:
, since it is only passed when the user swipes the notification:The key value for launching in response to a local notification is:
UIApplicationLaunchOptionsLocalNotificationKey
Source: UIApplicationDelegate class reference
Option 1: schedule one day at a time (Code for this is provided below)
One way to handle the scheduling of notifications is to present a schedule to the user, where the day's notifications are scheduled at the time of the initial opening of the app.
Use a
CustomNotificationManager
class to handle notifications whose times are variable (code provided below). In your AppDelegate, you can delegate to this class the handling of the local notifications, which will either schedule the current day's notifications plus the following day's fixed-time notification, or respond to a prayer notification.If the User opens the app in response to a prayer notification, the app can direct the user to an appropriate part of the app. If the user opens the app in response to the fixed-time notification, the app will schedule that day's local notifications according to the User's date and location.
Option 2 (Slightly slimmer approach, but which provides less to the User)
Another approach is to simply use a prayer notification's app launch to schedule the one that immediately follows. However, this is less reliable, and does not provide the ability to preview a schedule of notifications.
Notification Manager Header file
Notification Manager Implementation file
AppDelegate didReceiveLocalNotification Implementation
Suggestion for possible modification: If the CustomNotificationManager needs to maintain state, you could convert it to a Singleton.
The best way I found, so far, is to schedule the prayers for the next coming 12 days (12 days * 5 notifications = 60 notifications).
Once the user open the app, I remove all remaining notifications and re-schedule a new ones for the next 12 days.
The important thing the do, is to add a Background Fetch (job) to your application. In AppDelegate class add this code:
Modify didFinishLaunchingWithOptions method like this:
Here is the methods that schedule the 12 days notifications:
This is working fine for my Prayer Times app.
I hope this will help ;)
There are three ways to do this:
Use push notifications instead of local notifications and move the logic to the server. Problem - user won't get notifications when offline.
Keep using local notifications. You will have to plan a new notification for every prayer time. Of course, the number of local notifications is limited (max
64
scheduled notifications) but it should be enough for a week of notifications. A notification is not an alarm, the user is supposed to open the application in response to receiving a notification. That way, you can always reschedule all notifications when the app is reopened. Also, the last notification can be something similar to "you have not opened the app in a while, you won't be receiving more notifications".Instead of creating local notifications, create alarms/reminders in your device calendar (Event Kit)