sp309
sp309

Reputation: 150

How to get local notification when app is closed in iOS

I want notification at 2:00 pm and 6:00 pm even if app is closed.

How to check for particular time at which I want to fire local notification and where should I write that code in my code in iOS?

please suggest me a code in Objective-C.

Should I use this method?

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler

If yes then what should I write in this method for getting notification at particular time.

Upvotes: 0

Views: 2400

Answers (3)

Spartacus9
Spartacus9

Reputation: 174

application:didReceiveLocalNotification: is now deprecated because the class UILocalNotification is deprecated.

Use UNNotification instead.

Upvotes: 0

user3182143
user3182143

Reputation: 9589

You should not use didReceiveRemoteNotification method for getting local notification.

You should use didReceiveRemoteNotification method for getting push notification.

We must know the below things

When app is not running or when app is closed state

If the app is not frontmost and visible, the system displays

the alert

message, 

badges

the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched. (If the user taps one of the custom actions you specify using the category property, the app is woken up or launched into the background.) In its application:didFinishLaunchingWithOptions: method, the app delegate can obtain the UILocalNotification object from the launch options dictionary using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the app icon, and the user in response launches the app, the application:didFinishLaunchingWithOptions: method is called, but no UILocalNotification object is included in the options dictionary. When the user selects a custom action, the app delegate’s application:handleActionWithIdentifier:forLocalNotification:completionHandler: method is called to handle the action.

Apple API reference

When Applicaton is Running in Foreground State

If the app is foremost and visible when the system delivers the notification, the app delegate’s application:didReceiveLocalNotification: is called to process the notification. Use the information in the provided UILocalNotification object to decide what action to take. The system does not display any alerts, badge the app’s icon, or play any sounds when the app is already frontmost.

applicationDidReciveLocalNotification

Also

iOS: If app is in background and local notification is arrived; which method will called automatically?

Upvotes: 1

vaibhav
vaibhav

Reputation: 4096

When app is suspended or in background a delegate method didReceiveLocalNotification called and local notification comes inside:

-(void) application:(UIApplication *)application 
        didReceiveLocalNotification:(UILocalNotification *)notification  

You need to implement above method in appdelegate.

Please remind didReceiveRemoteNotification method only called whenever app getting notification through server called as push notifications or remote notification

Upvotes: 1

Related Questions