I would like to know how I can setup local notifications so that at the time I set, my app generates a notification/alert with a customized message...
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
iOS 8 users and above, please include this in App delegate to make it work.
And then adding this lines of code would help,
In appdelegate.m file write the follwing code in applicationDidEnterBackground to get the local notification
This is worked, but in iOS 8.0 and later, your application must register for user notifications using
-[UIApplication registerUserNotificationSettings:]
before being able to schedule and present UILocalNotifications, do not forget this.[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ApparelsViewControllerHide) name:@"ApparelsViewControllerHide" object:nil];
Creating local notifications are pretty easy. Just follow these steps.
On viewDidLoad() function ask user for permission that your apps want to display notifications. For this we can use the following code.
Then you can create a button, and then in the action function you can write the following code to display a notification.
Notification will be displayed, just click on home button after tapping the notification button. As when the application is in foreground the notification is not displayed. But if you are using iPhone X. You can display notification even when the app is in foreground. For this you just need to add a delegate called UNUserNotificationCenterDelegate
For more details visit this blog post: iOS Local Notification Tutorial
Here is sample code for LocalNotification that worked for my project.
Objective-C:
This code block in
AppDelegate
file :This code block in .m file of any
ViewController
:The above code display an AlertView after time interval of 7 seconds when pressed on button that binds
startLocalNotification
If application is in background then it displaysBadgeNumber
as 10 and with default notification sound.This code works fine for iOS 7.x and below but for iOS 8 it will prompt following error on console:
This means you need register for local notification. This can be achieved using:
You can also refer blog for local notification.
Swift:
You
AppDelegate.swift
file should look like this:The swift file (say
ViewController.swift
) in which you want to create local notification should contain below code:The way you use to work with Local Notification in iOS 9 and below is completely different in iOS 10.
Below screen grab from Apple release notes depicts this.
You can refer apple reference document for UserNotification.
Below is code for local notification:
Objective-C:
In
App-delegate.h
file use@import UserNotifications;
App-delegate should conform to
UNUserNotificationCenterDelegate
protocolIn
didFinishLaunchingOptions
use below code:Now create a button in any view controller and in IBAction use below code :
Swift 3:
AppDelegate.swift
file useimport UserNotifications
UNUserNotificationCenterDelegate
protocolIn
didFinishLaunchingWithOptions
use below codeNow create a button in any view controller and in IBAction use below code :