I m new to objective-c, i want to set local notification in my app.
App should be work with both IOS 8 and all less than IOS 8 version.
please help me with detail code.
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
#end
}
and you fire a local notification like this:
- (void)showNotificationWithMessage:(NSString *)message
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = message;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Related
I created a Local Notification that triggers 60 seconds when a certain button(SetButton) is clicked. My problem right now is if SetButton is pressed again, it does not override the first press, it displays 2 notifications and so on. How do I make sure that the second press of the button overrides the first press and there isn't a build up of notifications ?
- (IBAction)SetButtonPressed:(id)sender {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
My AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([UIApplication instanceMethodForSelector: #selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
if (localNotification) {
application.applicationIconBadgeNumber = 0;
}
}
If you only use notifications for that specific action you could cancel all notifications at once using
[[UIApplication sharedApplication] cancelAllLocalNotifications];
It looks like you only have one type of local notifications in your app.
In that case you could keep it simple. Whenever you want to schedule a new one - cancel the previous one.
- (IBAction)SetButtonPressed:(id)sender {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber =
[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
I'm building an app that allows its users to send each other messages. When a new message is received, it appears in the current user's Table view. I want my app to send the current user a notification when a new message arrives. Does anyone know how I might go about doing this?
I have notifications set up in my AppDelegate already:
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
and I know the below code will allow me to fire a notification at a specified time (e.g. set by a picker):
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"You are being notified!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
How do I make a notification occur however when a new message is posted to the server (and appears in the tableview) for the logged in user?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];}
If your system is iOS8, you need to register permission.
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:Time];
if (notification!=nil) {
notification.fireDate= pushDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = kCFCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = #"Time O.K!";
NSLog(#"Ready");
notification.applicationIconBadgeNumber = 1;
//notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
NSDictionary * inforDic = [NSDictionary dictionaryWithObject:#"name" forKey:#"key"];
notification.userInfo =inforDic;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
You can have a try,this code I can successfully use on my device.
I am stuck with the strange problem. I am making an alarm application.When i fire a local notification it works fine in simulator but when I compile the code in an iPhone it dose not working.Same code is working on an simulator and not responding on a iPhone.
notification code:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = fireDate;
localNotif.alertBody = #"Time to wake Up";
localNotif.alertAction = #"Show me";
localNotif.soundName = #"Tick-tock-sound.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
and save button code is:
- (IBAction)saveBtn:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSLog(#"time is %#",dateFormatter.timeZone);
dateFormatter.timeStyle = NSDateFormatterShortStyle;
NSString * dateTimeString = [dateFormatter stringFromDate:timePicker.date];
datesArray = #[[dateFormatter stringFromDate:self.timePicker.date]];
}
Please tell me the solution. Thanks
Write this code in appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
return Yes;
}
I want to fire local notification on particular time daily, i have wrote this code snippet but it is not receiving notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// running on iOS8
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"didReceiveLocalNotification----");
application.applicationIconBadgeNumber = 0;
}
//ViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:7];
[dateComponents setMinute:59];
NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
NSDate *fireDate = nil;
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = #"Notiication success....";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
My Log shows didReceiveLocalNotification but notification not appearing in device :(
Where i am making mistake please help.
Thanks in advance
The notification won't appear while app is in foreground. The notification will show up only if the app totally close or in the background.
Is it possible to setup an app to send notifications from it's self at specified times? Perhaps by putting them in a queue? As of now I'm having to go through a push notification server and that seems like over kill when I'm sending messages out every Tuesday morning. Also, what is this called (so I can better research it on the web)
You can schedule notification like this:
- (void)scheduleNotification
{
//Cancel all previous Local Notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Set new Local Notifications
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
//3 days
notif.fireDate = [NSDate dateWithTimeInterval:60.0f*60.0f*24.0f*3.0f sinceDate:[NSDate date]];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Come Back Again! Lets blast All Zombie!";
notif.alertAction = #"PLAY";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[self scheduleNotification];
..
}