How to check if Local Notification is enabled/disabled? - ios

I understand we can check if a user has enabled/disabled Remote Notification with this code:
[[UIApplication sharedApplication] enabledRemoteNotificationTypes]
But what about checking for Local Notification?
I don't find a corresponding property for local notification types, and I have verified that enabledRemoteNotificationTypes is only for remote notifications.
And we all know, users can edit their Notification settings, which will affect both remote and local.

I'm not sure, but I don't think you can access this information.
One way you can check if the user has notifications enabled for your app is to send yourself a local notification with a 1 second delay :
UILocalNotification *testNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = #"Test notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
And check if you catch it in :
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification) {
// If you get here, notifications are enabled
}
All there is left is add info (e.g in localNotification.userInfo) so you can know in didReceiveLocalNotification: if you are handling your test notification, or if it's a "real" notification.

- (BOOL) isLocalNotificationsEnable {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(currentUserNotificationSettings)]) {
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return (grantedSettings.types == UIUserNotificationTypeNone) ? NO : YES;
}
return NO;
}
Note: the if block is only require if you're targeting < iOS 8.0.

Related

Custom UIButton with local notification

I'm trying to create a custom button that then schedules a local notification. The code I've found on here seems to be outdated. I've looked at the outlines from Apple https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
But I'm still pretty confused. Can anyone post a clean and concise example including which files I put which code? (Including registering) Even just an example of creating any local notification would be helpful, thanks!
In AppDelegate.m, register the types of user interactions the app supports (from iOS 8.0 and later)
UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
Inside the button tapped method you do this:
NSDate * fireDate; // The schedule date time.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"alertText";
localNotification.alertAction = #"buttonTitle";
localNotification.soundName = UILocalNotificationDefaultSoundName;
// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
In AppDelegate.m add this method to handle when a local notification arrives (if necessary):
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification{
// This method gets called when a local notification arrives
}

How to generate faster local notification in iOS?

I am generating Local notification when Push notification(Actionable) are received but app is closed or in background. I have used following code to generate local notification in objective-c.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
// localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Security settings enabled, tap to start the application";
localNotification.category = #"LOCAL_NOTIFICATION"; // Same as category identifier
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
1.When i drag notification trey down(Notification Center) not from alert it generate local notification fine in 1 sec.
2.But while press action from alert(while at home) it
it takes 3-4 seconds for local notification to appear.
Why there is time difference between action from alert(press action from home) and Notification center(swipe down notification trey)
generating local notification?
How to make it faster? Thanks in advance.
Try to use the presentLocalNotificationNow method instead of scheduleLocalNotification to make your notification fire instantly:
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

Silent push notification doesn't work on iOS7 in background mode

I have this weird situation, my app support iOS7 and above. what it does, it's enabled Remote notifications by using silent notifications.
I know that iOS7 and iOS8 above has different logic to handle notification. I did this :
if ([[UIApplication sharedApplication] respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
here's notification receives
{
aps = {
"content-available" = 1;
};
}
So what it does is, app receive silent notification, and then set localNotification, see below :
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = #"testing";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
All it works in iOS8 and iOS9 in background mode and foreground mode. When app is in foreground, it will trigger didReceiveLocalNotification.
But when I was testing in iOS7, it doesn't work if the app is in background mode. I'm trying to figure out how this happen, while other OS working fine. I did test while app is open, it did receive push notification, and didReceiveLocalNotification get triggered. but when goes to background, nothing happen (no local push notification).
You didn't mention about enabling Background Fetch. Have you enabled it in your App Capabilities?
and set this in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
and this as your delegate method
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// do your thing here. maybe calling a view controller class or stuff
}
But if you're not into background fetch, try this instead:
{
aps = {
"content-available" : 1,
"sound" : ""
};
}
Good luck!
Im not exactly sure about the issue, but maybe you can try to change respondsToSelector. Refer to the following:
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)])
As pointed in this tread, aps need to include priority in order to work in iOS7.
aps = {
badge = 7;
"content-available" = 1;
priority = 5;
};
check this : https://stackoverflow.com/a/23917593/554740

Check if user allowed local notifications or not. iOS 8. Obj-C

Is there no simple way of testing if the user has allowed local notifications? I noticed a warning in my console after I denied the sending of local notifications. When the relevant event occurred it said that the app tried to send a notifcation even though the user didn't allow it. I want to check if it's allowed before attempting to display a notification. See the comment in my condition, how do I do that?
My code is:
app = [UIApplication sharedApplication];
-(void)showBackgroundNotification:(NSString *) message {
//check if app is in background and check if local notifications are allowed.
if (app.applicationState == UIApplicationStateBackground /*&& LocalNotificationsAreAllowed*/){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
I get prompt the user for permission like so:
UIUserNotificationSettings *settings;
if ([app respondsToSelector:#selector(registerUserNotificationSettings:)])
{
settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotification TypeSound) categories:nil];
[app registerUserNotificationSettings:settings];
}
couldn't I use settings object?
EDIT: I think I have solved it. This seems to work.
-(void)showBackgroundNotification:(NSString *) message {
if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
UILocalNotification *note = [[UILocalNotification alloc]init];
note.alertBody = message;
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
[app scheduleLocalNotification :note];
}
}
Here's what I use for less specific situations:
+ (BOOL)notificationsEnabled {
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return settings.types != UIUserNotificationTypeNone;
}
I usually keep a set of these types of methods in a notification manager.

Difference between presentLocalNotificationNow and scheduleLocalNotification

What is the difference between presentLocalNotificationNow and scheduleLocalNotification.
For the both following function is showing notification after 1 second
-(void)showLocalNotification:(NSNotification *)notification {
NSString *msg = #"test message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
_localNotification.alertBody = msg;
_localNotification.soundName = UILocalNotificationDefaultSoundName;
_localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
[[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
// or
//[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification];
}
If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by the application. In that case, you need to present the notification using presentLocalNotificationNow.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
[application presentLocalNotificationNow:notification];
}
}
From Apple Documentation:
Once you have created an instance of UILocalNotification, you schedule
it using one of two methods of the UIApplication class:
scheduleLocalNotification: or presentLocalNotificationNow:. The former
method use the fire date to schedule delivery; the latter method
presents the notification immediately, regardless of the value of
fireDate. You can cancel specific or all local notifications by
calling cancelLocalNotification: or cancelAllLocalNotifications,
respectively.
There's no difference right here, but using scheduleLocalNotification you can schedule it at whatever time you want, not only in one second.
And, of corse, nobody promises you that presentLocalNotificationNow will show one in exactly one second, not in 0.5 or 2.0 in iOS 8, for example.

Resources