Schedule iOS Push Notification with Parse.com - ios

I am trying to schedule push notifications using Parse in iOS. I only have a free account (meaning that I cannot schedule notifications in the web backend), so I came up with a solution for scheduling the notification through Parse:
Within the app, when you press send notification you set the text and a date to send the notification (using the code to just send a standard PFPush code). Then I just use NSDate and every day check the date to see if it matches the date for the scheduled notification. If it does, it sends the notification; thus sending the notification on the scheduled day, if it is not the matching day, nothing happens (no notification is sent, and the app rechecks in another 24hrs). This works, but the app has to be open in the foreground at the time that the notification is scheduled for sending (i.e the date is not checked and notification not sent if the app is in the background). So my actual question is how I can run all of this in the background, so the app can just be in the background on my iPad, and it will still check the date and be able to post the PFPush (push notification).
Any help or code would be much appreciated.
Thanks so much in advance, and let me know if you need more information!

In this case
setMinimumBackgroundFetchInterval means that it is not called in an interval which is smaller than the value you specified. But there's no setMaximumBackgroundFetchInterval. So if iOS decides to call your app just once a day or even just once a week than it won't be called more often regardless your minimumBackgroundFetchInterval. AFAIK iOS decides when to call performFetchWithCompletionHandler measured by the pattern when and how often the users start's the app.
In Xcode 5 Debug mode, you can force a background fetch from the menu: Debug > Simulate Background Fetch.

Related

iOS Firebase/APNS push notification sound according to local time or timezone

I am using FCM to send iOS push notifications for an app that is used worldwide. Different users are in different time zones.
Is there a way to specify ,that when delivered at worktime local time Mon-Fri (8:00 - 17:00) it will have sound, but any other it will be without sound. I do not want to disturb the user at night.
This question is mostly about the worst situation when my app isn't running/backgrounded/suspended (for example after reboot) and/or offline
.My app uses background fetch, but that doesn't guarantee that it will be running at the delivery time of the push notification.
The problem is i do not know in advance when the push notification will be delivered. If i knew i would specify the sound key in the JSON.
The user could be offline when the push notification is sent.
For example, i send the push notification at night without sound, but the user enables internet connection at day and receives the notification without sound (because it was sent in the night)
Scheduling the sending time at the server won't solve this, because we cannot control the delivery time.
If it is possible using directly APNS without Firebase then that would also solve my problem.
Thank you very much.
Clarification:
When my app is running i have enough control to decide according to local time if the sound should be played. The question is aimed at the other case when my app isn't running/backgrounded/suspended.
I am using silent data notifications (JSON key data with content-available=1) to deliver all neccessary info, but when my app is not running there is only one way to send a notification that will be shown - that is a Display notification (JSON key notification)

Get date of when remote notification was received

I'd like to be able to get the date of when a remote notification was received on an iOS device.
I'm attempting to determine the amount of latency between when my server fires a push notification to when it's received on a user's device.
In application:didReceiveRemoteNotification:fetchCompletionHandler: of you application delegate, create a NSDate with the current date, then save it inside an array in NSUserSettings or directly inside.
With this you even have an history of received notification and can process them later.
application:didReceiveRemoteNotification:fetchCompletionHandler: will call in the background only when you have added content-available key with value 1 into the notification payload.
From Apple documentation :
For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
You could add a custom sent-date field to the notification payload, if you control the back-end connected to APNS, and parse for it in didReceiveRemoteNotification. Obviously this will let you get the date when a remote notification was sent, not received, but in general the difference is small.
The solution suggesting to use the 'content-available' key may not be what you want, as it launches the app in the background on such notifications and may lead to rank your app as top battery offender in Settings.app > Battery > Battery Usage. It would also stop working in Low Power Mode, or if the user disables Background App Refresh.
Background App Refresh and the 'content-available' key are meant for apps that download content in the background. If your app isn't doing that, don't use them.
Create and initialize NSDate object in didReceiveRemoteNotification. NSDate *notificationDate = [NSDate date] should work.
You can see when an Push Notification is received by looking at the system(not app) console. It seems newer iOS versions filter the log, but if you kill your app beforehand you'll see something like this
Jun 1 17:55:38 Aarons-iPhone-6-Plus SpringBoard[657] <Warning>: High Priority Push: net.test.test - App killed –

how to start my App in a particular time of the Day in Swift iOS

I have an App built on Swift, I want this App to start every day at some particular time. Logically its like Calendar notification, which gives notification in that particular window whatever we set.
Does is the same scenario is possible with an App in iOS Swift.
What you can probably do is to create a local notification, but this is not opening your app. A local notification is just a way to show a notification on your iPhone and then, if the user taps, it's opening your app.
See more here: https://www.codebeaulieu.com/49/How-to-add-local-notifications-to-your-app
I am not sure what you want to do, you cannot force your application upon the user without the user's consent. What you can do is schedule a local notification so the user knows when to open your app like jomafer proposed already. Also possible is to wake up the app to do stuff in the background:
https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
For example a silent push notification that will trigger some code, or a scheduled background download.
NONE OF THESE METHODS ARE 100% RELIABLE THOUGH!

Cancelling local notification on specified date

I have problem with even inventing the posible solution for this problem.
My app needs to crate some objects of UILocalNotification with interval but if I set interval to be NSMinuteCalendarUnit if will bother me every minute until I cancel this notification in UIApplication or crash my phone with hammer...
But I'm setting in my app an end date for my project and I want those notification to work only to a specified date. Someone told me to check it when the app launches (not a problem but what if I set this notification to be fired every minute and I won't close my app during this time?). I need something independent and I want to know how to do this.. Any ideas?
You could check if you need to cancel the local notification in receiving one when you app is running and on the start up of you app.
When your app is running in the foreground and a local notification is fired for you app the application:didReceiveLocalNotification: is called on your App delegate.
There is not end date for local notifications thus the only way you can cancel then is from you app when it is running.

Can push notifications be used to run code without notifying user?

I have an app which needs to communicate with a server (to refresh it's data) once every 24 hours. This needs to happen even if the app is not open, nor in the background.
Ideally what I'd like is:
Every 24 hours, my server sends a push notification to iPad
This wakes up the app, and runs the code necessary to refresh the data
The notification is then discarded
Is any of this possible?
Is the app only woken up AFTER the user clicks on the notification? Or can I run some code before showing the notification?
Can I even discard the notification message?
If Push notifications are not the right way to do this, what is???
Thanks guys!
With release of iOS 7 this is finally possible using Remote/Silent notifications. They work same as Push notifications but instead of alerting the user immediately they can fire up background fetch mode and upload/download new content.
Here is simple tutorial:http://www.objc.io/issue-5/multitasking.html
This is only possible if your application is a Newsstand App, if it is, you can send a push notification with content-available: true once every 24 hours.
If it is not a newsstand app, you can instead use GPS fences to run code. It is allowed to let the user set up GPS fences if they want the app to update when the user comes close to an area (ie. their home / work). This will wake the app and you can run the background download then. Instapaper for example does this.
if the period is always going to be 24 hours you can use local notifications instead of push notifications.
your app won't be able to run any code unless the user open the push notification, push notifications are handled by the OS and your app have no control on them while it's not active or in the background you should take a look at the push notification programming guide
an alternative way to accomplish what you are trying to do, is to get the data from the server every time the app is started and the last update happened 24 hrs earlier ,
every time you contact the server save the date in NSUserDefault
every time the user opens the app check if the last server contact happened before 24 hours then refresh data
It is possible using a so called VOIP Push Notification. This notification can run code in the background, even when the app is completely closed.

Resources