Customizing UILocalNotification's alert
You can't customize the local notification. It's only text + 1 or 2 buttons.
For further details, you can go to Apple's local and remote notifications reference library.
Ask for User Permission to Receive UILocalNotifications in iOS 8
Since iOS 8 you need to ask user's permission to show notifications from your app, this applies for both remote/push and local notifications. In Swift you can do it like this,
Update for Swift 2.0
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
{
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "INVITE_CATEGORY"
notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)
//registerting for the notification.
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound, .Alert, .Badge], categories: nil))
}
else
{
//do iOS 7 stuff, which is pretty much nothing for local notifications.
}
return true
}
Swift 3.2
if(UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))){
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "INVITE_CATEGORY"
notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)
//registerting for the notification.
application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil))
}
else{
//do iOS 7 stuff, which is pretty much nothing for local notifications.
}
Objective C syntax is also very similar.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
// Override point for customization after application launch.
return YES;
}
To check for currently registered notification types you can use UIApplication class's method,
- (UIUserNotificationSettings *)currentUserNotificationSettings
So if the user has said no to your app then this function should return a setting without any types in it.
I have written a tutorial about this, you could see it here.
Show an image in the alert body of a local notification
I also searched for the same question. And found that we can't customize the UILocalNotification
, so I handled this in application:didReceiveLocalNotification:
by showing custom UIAlertView
.
How to set an image and sound for local notifications?
It is not possible to customize local notifications. You can just change the text and add one or two buttons. Please, refer to this question for more insight.
UILocalNotification Scheduling an Alert
If the notification shows up right away there probably is something wrong with your fireDate property. It looks like you are trying to verify that the date sent is correct but you should also verify that that fireDate is what you expect.
Also, you could perhaps do
localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];
to achieve the same effect and don't have to mess around with NSDateComponents
. I have not tested this but it might work.
As for what happens when the timer fires and your app is not running. If the app has not been terminated application:didReceiveLocalNotification:
will get called. If on the other side your app has been terminated you need to check in application:didFinishLaunchingWithOptions:
as pointed out by @Ishu. I usually just pass the notification to a common method to avoid duplicating the code.
Related Topics
Is Wkwebview Designed as a Replacement of Uiwebview
Type 'String.Index' Does Not Conform Protocol 'Integerliteralconvertible'
How Get the List of Paired Bluetooth Devices in Swift
How to Convert String to Unicode(Utf-8) String in Swift
Flutter iOS Build Failure Error with Multiple Commands After the Xcode Upgrade
iOS 5 Gm: <Error>: More Than Maximum 5 Filtered Album Lists Trying to Register. This Will Fail
Healthkit Authorisation Status Is Always 1
Uicontrolstate.Normal Is Unavailable
What Does It Mean for Something to Be Thread Safe in iOS
Why Does "Try Catch" in Objective-C Cause Memory Leak
Why Isn't Preferredcontentsize Used by iPhone 6 Plus Landscape
iOS How to Dismiss Uialertview with One Tap Anywhere
Perform Segue with Identifier Wont Work in Swift 2
How to Get the Scnview Camera Position When Using Allowscameracontrol
<Googlemaps/Googlemaps.H> File Not Found Google Maps Sdk for iOS
How to Get Vcf Data with Contact Images Using Cncontactvcardserialization Datawithcontacts: Method