i'm looking for work sound on notification with soundName in Objective-C on test project, but this doesn't work.
I used the code from how to create local notifications in iphone app answer, but sound don't work, i don't understant why :(.
In my ViewController.m
- (IBAction)startLocalNotification:(id)sender {
NSLog(#"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
And in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
}
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"This local notification"
delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
}
make sure your app is in background so you can hear sound otherwise you cannot able to hear the sound.
Related
I'm developing an iOS App which makes a local notification as an Alert on screen inside the app. I want it to send the notification to the main Notifications area and not alert anything on screen instead. How can I do this?
At the moment the notification area just says "No Notifications".
Here's my code:
AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
ViewController
self.itemText.text = #"Notification";
NSDate *pickerDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5];
NSLog(#"Picked date is %#", pickerDate);
NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(#"Todays date is %#", todaysDate);
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
// TO DO : Assign proper text to self.itemText.text based on real data
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"Another";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
UILocalNotification will be added to the main Notifications area only if application is in background or terminated. In the foreground it will call your method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
The only way you can get what you want, is to use UserNotifications.framework (since iOS 10).
Introduction to User Notifications Framework in iOS 10 is just the first tutorial
Here is the Apple Documentation on Local Notifications
And using it, don't forget to implement method of UNUserNotificationCenterDelegate to get Native notification even if your application is in foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
Hope it helps!
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.
my requirement is to fire notification message like popup in iPad, message i am getting from web service. i am calling web service after certain time interval and only if user to be notified then only i have to show notification.
my code is not working, i want like pop up. my code is below:
- (void)scheduleNotificationWithInterval:(NSString *)Notificationmsg
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"MyApp!";
localNotification.repeatInterval = nil;
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(Notificationmsg, nil)];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.applicationIconBadgeNumber = 1;
/*
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
localNotification.userInfo = infoDict;*/
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Please help me.
Try this one:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
put this method in Appdelegate class.
When Your fire localnotification and app is open Active state it will call this method and if app is in background mode it will show notification i.e what you style you have set for your app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSLog(#"lcoal:%#",[notification userInfo]);
UIAlertView *al=[[UIAlertView alloc]initWithTitle:#"Challenge gehaald!" message:#"Gefeliciteerd, je hebt deze bonus challenge succesvol afgerond." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[al show];
}
}
I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert...
What can you do to always get the sound and the alert notification ?
this is what i have in my view
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = nil;
localNotif.hasAction = YES;
localNotif.alertBody = fbName;
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
and this is the app delegate
- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
return YES;
}
If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your 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];
}
}
I'm developing an iOS application, and I have to get local notification, BUT in case of application's state is inactive. I successfully get notifications when application is in background state.
So, is it possible to get local notification, when application is inactive?
Or maybe that's possible only by using push notification?
Regards,
Armen
You need to respond to local notifications in two places in your app delegate:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
The first is for when your app was not running - use the launchOptions parameter to check if your app was launched due to a local notification.
The second is for when your app is currently running (active or inactive). You can check if the app is inactive by checking NSApplication's applicationState property in the application:didReceiveLocalNotification: method.
- (void)sendNotification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// notification.repeatInterval = NSDayCalendarUnit;
localNotification.fireDate = vwPicker.date;
localNotification.alertBody = txtAlarmTitle.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.userInfo = #{#"Title": txtAlarmTitle.text};
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self handleNotification:notification application:application];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification)
[self handleNotification:localNotification application:application];
return YES;
}
-(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application
{
NSString *title = [notification.userInfo objectForKey:#"Title"];
[[[UIAlertView alloc]initWithTitle:#"Smart Alarm" message:title delegate:self cancelButtonTitle:#"Answer the Teaser" otherButtonTitles: nil] show];
application.applicationIconBadgeNumber = 0;
}
Sure, just use willResignActiveNotification notification listener as listed
here