I am using badge notifications in my app and it works fine but I get the badge number by calling a method. So if method is called then the badge number gets increased but how to call that method while app is closed.
- (void)repeatedMethod {
SOWObject *object =[[SOWObject alloc]init];
[object getBadgeNumber:[self getDBPath]];
// I get badgeArray from above method
[UIApplication sharedApplication].applicationIconBadgeNumber=badgeArray.count;
}
is there any way we can call this method each day when date is changed and update badge number.
So far as I know, you can do this with 3 options:
Use Silent notification - for iOS7 and above only. a bit complicated since you need to enable Push and do back-end intergration
Use Background refresh - Create a timer + UIBackgroundTaskIdentifier. (not 100% sure)
Use Local/Push Notification - disadvantage using this, user knows of such notification is triggered.
implement this delegate method in your appdelegate:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
This method is fired whenever the OS finds any local notifications.(it doesn't matter whether your app is in the background or in the foreground).
For more information & code look here
Look at the accepted answer.
Edited:
Answer for you to call a method while app is in background is here
So, it basically says not all the apps have access to background execution. Officially it's mentioned in the apple's developer site also: HERE
Related
PeopleInTheKnow!
I'm writing this app, that uses Background Fetch, to see if there's some new information on a server.
Background Fetch works fine, but is pretty unpredictable in when it performs its trick.
Therefore, I though I should use scheduled UILocalNotifications, so I have more control over the exact timing and frequency of the execution of the method associated with Background Fetch.
But this is where I get confused:
My scheduled UILocalNotifications fire as expected. But the delegate
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
NSString * notificationType = [notification.userInfo valueForKey:#"dailyCheck"];
NSLog(#"Received Notification of type %#",notificationType);
}
in which I would like to call the server check method, is only called when the app is active.
This seems to make such a mechanism useless for my case.
Could any of you advise me what would be the best approach, to make sure that my app, be it in the foreground or background, will check the server every day at a a specific time?
I should add, that I don't expect any user interaction here. So, no alerts with buttons or anything like it.
Thanks ahead
My app receiving push notification, and showing appropriate info message for that. However when I'm clicking to the message, application becomes active but application didFinishLaunchingWithOptions is not getting called which is right i think, since the application is not suspended and it just resigns active. The question is how i can make sure that user clicked to message when application becomes to foreground ?
I think what you are looking for is this app delegate method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
It will be called if your app is backgrounded, and the notification payload will be delivered in the userInfo dictionary. This contrasts with the situation when the app is launched from cold start, when this method does not get called, and instead you check in the launchOptions dictionary for the payload.
However the preferred way to do this since iOS7 is to use this:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
This method is called when a user taps on a notification, regardless of whether the app is launched from cold start or foregrounded from background. So even if you are not using the completionHandler, it provides a more consistent way of accessing the notification payload. If this method is present, the older one does not get called.
If I understand the question correctly, you are asking how to be sure that the app was brought into the foreground as the result of the user “clicking” i.e. acting on a push notification.
When the app is not running at all, you can use -application:didFinishLaunchingWithOptions: as you mention. The launchOptions dictionary contains the payload, etc. — I won’t describe this since you already know how this works.
When the app IS running however, that method is not going to be called. Instead, - application:didReceiveRemoteNotification: is called. BTW this is called if the app was already in the foreground OR if it was in the background and the user “clicked” on the push notification banner/alert to open the app. It will not be called otherwise: so I believe this is exactly what you’re looking for.
The userInfo dictionary provided by this method will contain the notifications data, similarly to -application:didFinishLaunchingWithOptions: for more ad-hoc processing. (Note: userInfo and launchOptions are semantically different, but hopefully this is obvious. :))
In birthday app which is the best place to check birthdates and set its UILocalNotification in iOS.
Iss didfinishlaunching the best place? or any other.
That is a good place to schedule the notification if you need to check it only once.
If data changes often I suggest you put it in applicationDidBecomeActive: since iOS might not remove the app from memory each time.
You also need to make sure that you schedule only one notification, and not each time the application is started.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"Notification fired"!);
}
if your in foregroud it will call this method. then you can use this method also.
Im a newbie to iOS. I need some guidance on which is the best way to retrieve json every 5 sec from a remote server both, when in foreground or background, so that the UI gets updated properly regardless of the state. For example: Similar to a live cricket score app for iOS.
Any leads will help me. Thanks.
You should use background fetching, a system-supplied way of efficiently polling. Think in terms of minutes to hours between each poll. Otherwise, use silent push notification: it will wake up the app to do actual downloading. Read on if you are trying to do this for an actual app that you want in the app store.
Facebook app uses this push-to-download AND also abuses this feature to poll to often, thus draining the battery. That is why most people will turn this feature off for Facebook and hence there is then NO polling at all anymore. So be careful with how much energy you consume for a real app.
For fetching data from background you can use BackgroundFectch mechanism.
All you need to do is
In appdelegate class you have to set the time interval.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];//default fetch interval is never,
return YES;
}
Enable the app for background fetch
Target->Capabilities->backgroundModes->backgroundFetch
3.Final step is implement the following method in app delegate class.
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
Hope this will help you .
There is one sample app in Gitgub in which NSXmlParser is used .You can refer that .
You should use NSTimer for repeatedly performing actions::
The following class method will help you:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
target:(id)aTarget
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
Use repeats:YES.
In iOS 7 you have a new background execution method which helps in a situation like this.
Apps that use push notifications to notify the user that new content is available can fetch the content in the background. To support this mode, include the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file. You must also implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method in your app delegate.
So you could send push notification from a server in a timely manner so app can be configured to do the background execution and fetching data.
I was wondering if in iOS7, with the new API's it was finally possible to respond to a notification in the background, in my case, I have the following observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(storeChanged:)
name:EKEventStoreChangedNotification
object:eventStore];
I am receiving the notification perfectly but i need to run the app so that the selector gets called. I've browsed through the response and they say it's not possible but not sure if they where referring to iOS7 specifically.
Any thoughts?
Thanks!
The EKEventStoreChangedNotification will only fire when your app comes to the foreground. However if you want to call your storeChanged: method in the background, and thus having the UI already updated on coming to foreground again, you need to add the Background Fetch capability to your app.
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
In your app delegate didFinishLaunchingWithOptions method add the line
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
This ensures your app actually calls your background fetch, as the default interval is never. This minimum key is the key that ensures iOS handles when to call your background fetch method. You can set your own minimum interval if you don't want it to fire as often as possible.
Finally implement the background fetch method in your app delegate:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[self storeChanged:nil];
completionHandler(UIBackgroundFetchResultNewData);
}
You can test in Xcode while debugging from Debug > Simulate Background Fetch.
Firstly when the app is in the background you can only run methods using the background task APIs to call a method after you've been backgrounded (as long as your task doesn't take too long - usually ~10 mins is the max allowed time). This applies to all versions of iOS even iOS7.
Read this question for more clarifications.
App States and Multitasking Guide by Apple can give you more clarifications on background handling.