2

I've been looking for the solutions for several hours. I know how to use local notification. What I found is that local notification can be shown repeatedly, but the notification body and icon badge number should always be the same. What I want to do is: check a condition repeatedly even if the app is not running, if the condition is true, put a notification to the notification center, show a number to the app's icon badge. The number to show is got from database.

Does anybody know how to handle this? Any help will be appreciated.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Vigor
  • 1,706
  • 3
  • 26
  • 47
  • If the condition has to be loaded from an HTTP server, you can use background fetch, but the interval you specify is just a suggestion, iOS won't let you run it more than once every few hours. Otherwise, you can't. There has to be a server sending a notification through the Apple notification service. – Jano Oct 12 '13 at 16:06
  • I don't want to use server because the content to show is stored in local database. the interval is just a suggestion, run it once every day is ok too. – Vigor Oct 12 '13 at 16:10
  • You could use Background Fetch, but that's designed to fetch something from the Internet. The description is “The app regularly downloads and processes small amounts of content from the network.”. If you cheat and use it for something else, your application may not get approved. You could download something from a server to simulate meaningful network activity. – Jano Oct 12 '13 at 16:18
  • The relevant Apple doc is [Implementing Long-Running Background Tasks](https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24). – Jano Oct 12 '13 at 16:24
  • I haven't tried yet, so can hussain Shabbir's answer solve this problem? – Vigor Oct 12 '13 at 16:35
  • Not at all. You should at least start a background task when the app is dismissed, as in http://stackoverflow.com/q/3871149/412916 But that will only work for 10 minutes. – Jano Oct 12 '13 at 18:17

1 Answers1

0

Well you can do this, use nstimer and then run it every minutes after that inside timer selector write your condition to check after that on the basis of that post notification.

   [NSTimer scheduledTimerWithTimeInterval:1
  target:self select:@selector(sendnotification:)    userInfo:nil repeats:YES];

   -(void)sendnotification:(Nstimer*)timer
   {
   //post notification here
    }
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Thank you for your answer. Would you please give me more details. like where to put this NSTimer? Is it work even when the app is not open? Thx! – Vigor Oct 12 '13 at 15:27
  • This you have to look on you app where you want basically. I would like to suggest inside the method when launch the application. – Hussain Shabbir Oct 12 '13 at 15:36