Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS/Obj-C - Fire local notification without timer?

I want to setup some local notifications in my iOS app, however every tutorial I seem to find on implementing these seems to only allow me to fire a notification based on a timer (see sample code below)? Is it possible to fire a local notification for example when new data is loaded to a UITableView? Sorry for the general question, but I can't seem to find a whole lot of documentation on this. I'm also not sure how I could do this if data is only grabbed when my user hits a screen? E.g. data is grabbed/updated in viewDidLoad of ViewController?

-(void)viewDidLoad {

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = dateTime;
        localNotification.alertBody = [NSString stringWithFormat:@"Alert Fired at %@", dateTime];
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber = 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    }
like image 607
Brittany Avatar asked Mar 28 '17 16:03

Brittany


2 Answers

First of all you should consider the fact that UILocalNotification class that you are using is deprecated starting from iOS 10. Since iOS 10 there is no distinction between local and remote notifications.

In UserNotifications framework (iOS 10.0+, Swift | Objective-C) one does not create instances of UNNotification like in some of the previous implementations of similar frameworks / API's by Apple. Instead, an instance of UNNotificationCenter is responsible for creating notification objects and calls delegate methods when new notifications are received and before the default UI will be displayed. See Apple's documentation for more details on the requirements for conforming UNUserNotificationCenterDelegate protocol. I often end up using AppDelegate as a class conforming to that protocol

Now back to the question. To initiate a creation of the notification from within the app first you should create a trigger (in your case you might want to use UNTime​Interval​Notification​Trigger), then create a notification request and finally add the request to the notification center singleton that can be accessed by calling UNUserNotificationCenter.current().

let content = UNMutableNotificationContent()
content.title = "Alert Fired"

Create a trigger that will determine the moment when UNUserNotificationCenter will invoke the notification

let trigger = UNTime​Interval​Notification​Trigger(timeInterval: 0, repeats: false)

Create a notification request. You only create those to invoke local notifications, since iOS is responsible for creating request objects for incoming push notifications.

let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)

Now we will have to add our request to the current notification center associated with our app.

let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)

In our case the notification will be triggered immediately by the UNUserNotificationCenter.

like image 78
Kirill Dubovitskiy Avatar answered Oct 14 '22 14:10

Kirill Dubovitskiy


You can use the method postNotificationName like this:

MyCustomObject *customObject = [MyCustomObject new];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Your notification name" object:customObject];

Processing the object coming through from the posted notification would look something like this. Obviously, I am only logging here but you can process it whatever way you like.

- (void)methodToProcessNotificationData:(NSNotification *)notification {
   NSLog(@"%@",notification.object);
}

When you want a class to listen for the notification you can do something like the below chunk of code. This adds the observer to the class, gives it the method that is to be called (in this instance methodToProcessNotificationData) and the notification name that you wish to listen out for.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToProcessNotificationData:) name:@"Your notification name" object:nil];
like image 27
HarmVanRisk Avatar answered Oct 14 '22 14:10

HarmVanRisk