0

I am trying to implement local notifications on an app that I am building. Most of my code follows what is written In the documentation

I will post my code below. My current problem is that the notifications never appear. The first time I loaded the app the permission screen appeared and I said "Allow"

In AppDelegate in the didFinishLaunchingWithOptions method

didFinishLaunchingWithOptions

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
    [self setupNotification];
}];

The following is also in AppDelegate

-(void)setupNotification {
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = [NSString localizedUserNotificationStringForKey:@"New:" arguments:nil];
        content.body = [NSString localizedUserNotificationStringForKey:@"New Notification"
                                                             arguments:nil];

        content.sound = [UNNotificationSound defaultSound];


        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:5
                                                      repeats:NO];


        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NOTIFICATION"
                                                                              content:content
                                                                              trigger:trigger];


        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];


        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"success");
            }
        }];
    }

As I said before, the notification never appears and I cant figure out why. I set up the content, the trigger and the request, then I add the request to the UNUserNotificationCenter.

Does anyone have a working example of this or can tell me where I am going wrong?

I found a similar answer here but this answer doesnt address why UNTimeIntervalNotificationTrigger isnt working and instead explains how to set up a UNCalendarNotificationTrigger

Thanks

Community
  • 1
  • 1
Nate4436271
  • 860
  • 1
  • 10
  • 21

1 Answers1

0

UNUserNotificationCenter Swift 3 Implementation and Usage:

//  AppDelegate.swift

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    // Override point for customization after application launch.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Setup Notifications
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (bool, error) in

        }

        return true
    }

Create an "Extension.swift" file, and add:

//  Extensions.swift

import UIKit
import UserNotifications

extension UNUserNotificationCenter {

    func scheduleNotification(identifier: String, body: String, time: Double) {
        let content =  UNMutableNotificationContent()
        content.body = body
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request) { (error) in
            if (error != nil) {
                print(error!)
            } else {
                print("Success! ID:\(identifier), Message:\(body), Time:\(trigger.timeInterval.magnitude) seconds")
            }
        }
    }
}

In any view controller, declare under "class" (above viewDidLoad):

var notificationCenter: UNUserNotificationCenter?

In "viewDidLoad" function, initialize UNUserNotificationCenter:

notificationCenter = UNUserNotificationCenter.current()

Call Notifications from any function within the view controller:

// Use any identifier, Message and time (in seconds)
self.notificationCenter?.scheduleNotification(identifier: "welcomeScreen", body: "Welcome to my App!", time:20)

This should enable Local Notifications in Swift 3 and iOS 10.

Video Reference: https://youtu.be/Svul_gCtzck

Arshin
  • 1,050
  • 1
  • 9
  • 10