Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions LocalNotificationBase.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
869E664C22B2C8A4000E5866 /* EditViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 869E664B22B2C8A4000E5866 /* EditViewController.swift */; };
869E665022B2C958000E5866 /* sendNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 869E664F22B2C958000E5866 /* sendNotification.swift */; };
869E665222B2F071000E5866 /* logo.png in Resources */ = {isa = PBXBuildFile; fileRef = 869E665122B2F071000E5866 /* logo.png */; };
869E665422B2F70E000E5866 /* HaViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 869E665322B2F70E000E5866 /* HaViewController.swift */; };
AA6671C922B1292D00824252 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA6671C822B1292D00824252 /* AppDelegate.swift */; };
AA6671CB22B1292D00824252 /* BaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA6671CA22B1292D00824252 /* BaseViewController.swift */; };
AA6671CE22B1292D00824252 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA6671CC22B1292D00824252 /* Main.storyboard */; };
Expand All @@ -15,6 +19,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
869E664B22B2C8A4000E5866 /* EditViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditViewController.swift; sourceTree = "<group>"; };
869E664F22B2C958000E5866 /* sendNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = sendNotification.swift; sourceTree = "<group>"; };
869E665122B2F071000E5866 /* logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo.png; sourceTree = "<group>"; };
869E665322B2F70E000E5866 /* HaViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HaViewController.swift; sourceTree = "<group>"; };
AA6671C522B1292D00824252 /* LocalNotificationBase.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LocalNotificationBase.app; sourceTree = BUILT_PRODUCTS_DIR; };
AA6671C822B1292D00824252 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
AA6671CA22B1292D00824252 /* BaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseViewController.swift; sourceTree = "<group>"; };
Expand All @@ -35,6 +43,26 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
869E664D22B2C8C3000E5866 /* View */ = {
isa = PBXGroup;
children = (
AA6671D122B1293000824252 /* LaunchScreen.storyboard */,
AA6671CC22B1292D00824252 /* Main.storyboard */,
);
path = View;
sourceTree = "<group>";
};
869E664E22B2C8CF000E5866 /* Controller */ = {
isa = PBXGroup;
children = (
869E664B22B2C8A4000E5866 /* EditViewController.swift */,
AA6671CA22B1292D00824252 /* BaseViewController.swift */,
869E664F22B2C958000E5866 /* sendNotification.swift */,
869E665322B2F70E000E5866 /* HaViewController.swift */,
);
path = Controller;
sourceTree = "<group>";
};
AA6671BC22B1292D00824252 = {
isa = PBXGroup;
children = (
Expand All @@ -54,11 +82,11 @@
AA6671C722B1292D00824252 /* LocalNotificationBase */ = {
isa = PBXGroup;
children = (
869E664D22B2C8C3000E5866 /* View */,
869E664E22B2C8CF000E5866 /* Controller */,
AA6671C822B1292D00824252 /* AppDelegate.swift */,
AA6671CA22B1292D00824252 /* BaseViewController.swift */,
AA6671CC22B1292D00824252 /* Main.storyboard */,
869E665122B2F071000E5866 /* logo.png */,
AA6671CF22B1293000824252 /* Assets.xcassets */,
AA6671D122B1293000824252 /* LaunchScreen.storyboard */,
AA6671D422B1293000824252 /* Info.plist */,
);
path = LocalNotificationBase;
Expand Down Expand Up @@ -122,6 +150,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
869E665222B2F071000E5866 /* logo.png in Resources */,
AA6671D322B1293000824252 /* LaunchScreen.storyboard in Resources */,
AA6671D022B1293000824252 /* Assets.xcassets in Resources */,
AA6671CE22B1292D00824252 /* Main.storyboard in Resources */,
Expand All @@ -135,7 +164,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
869E665422B2F70E000E5866 /* HaViewController.swift in Sources */,
AA6671CB22B1292D00824252 /* BaseViewController.swift in Sources */,
869E664C22B2C8A4000E5866 /* EditViewController.swift in Sources */,
869E665022B2C958000E5866 /* sendNotification.swift in Sources */,
AA6671C922B1292D00824252 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -283,7 +315,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = GT48V5L69E;
DEVELOPMENT_TEAM = CTAFH43K36;
INFOPLIST_FILE = LocalNotificationBase/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -301,7 +333,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = GT48V5L69E;
DEVELOPMENT_TEAM = CTAFH43K36;
INFOPLIST_FILE = LocalNotificationBase/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
98 changes: 94 additions & 4 deletions LocalNotificationBase/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let notificationCenter = UNUserNotificationCenter.current()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let options: UNAuthorizationOptions = [.alert,.sound,.badge]
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self

let options: UNAuthorizationOptions = [.alert, .sound, .badge]

notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
if !didAllow {
print("Notifications not allowed by user")
print("User has declined notifications")
}
}
return true
Expand All @@ -40,6 +42,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
application.applicationIconBadgeNumber = 0
}

func applicationDidBecomeActive(_ application: UIApplication) {
Expand All @@ -53,3 +56,90 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

}

extension AppDelegate: UNUserNotificationCenterDelegate {



func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

completionHandler([.alert, .sound])
}



func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {

//TAKE REPONSE OF THE ACTION
let identifier = response.actionIdentifier


//TAKE OPTION OF THE ACTION
if identifier == "repeat"{
scheduleNotification("title", "String", "String", "identifier")
}
else if identifier == "open" {

//Create a instance of the ViewController
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: HaViewController = storyboard.instantiateViewController(withIdentifier: "HaViewController") as! HaViewController;

// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);

}

completionHandler()
}



func scheduleNotification(_ title:String, _ subtitle:String, _ body:String, _ identifier:String) {

let content = UNMutableNotificationContent()

//CREATE BODY OF THE NOTIFICATION
content.title = title
content.subtitle = subtitle
content.body = body
content.sound = UNNotificationSound.default
content.badge = 1
content.categoryIdentifier = identifier


//SET IMAGE
let imageName = "logo"
guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return }
let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none)
content.attachments = [attachment]




//REQUEST NOTIFICATION
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

notificationCenter.add(request) { (error) in
if let error = error {
print("Error \(error.localizedDescription)")
}
}



//ACTIONS IN THE NOTIFICATION
let snoozeAction = UNNotificationAction(identifier: "repeat", title: "Repeat", options: [])
let deleteAction = UNNotificationAction(identifier: "open", title: "Open", options: [.foreground])
let category = UNNotificationCategory(identifier: identifier,
actions: [snoozeAction, deleteAction],
intentIdentifiers: [],
options: [])

notificationCenter.setNotificationCategories([category])
}
}
39 changes: 0 additions & 39 deletions LocalNotificationBase/Base.lproj/Main.storyboard

This file was deleted.

47 changes: 0 additions & 47 deletions LocalNotificationBase/BaseViewController.swift

This file was deleted.

25 changes: 25 additions & 0 deletions LocalNotificationBase/Controller/BaseViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// ViewController.swift
// LocalNotificationBase
//
// Created by Bruno Omella Mainieri on 12/06/19.
// Copyright © 2019 Bruno Omella Mainieri. All rights reserved.
//

import UIKit
import UserNotifications

class BaseViewController: UIViewController {
var appDelegate = UIApplication.shared.delegate as? AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

@IBAction func remindButton(_ sender: Any) {
// sendNotificationLocal("Lembre-se", "ocê se lembrou", "5", 5, true, 1)
self.appDelegate?.scheduleNotification("title", "subtitle", "body", "identifier")
}

}

Loading