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];
}
}
Related
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.
I know how to show a push notifications UIAlertView if the app is in the background or foreground using the following code:
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = [ [userInfo objectForKey:#"aps"]
objectForKey:#"alert"];
UIAlertView *alert = [ [UIAlertView alloc]
initWithTitle:#""
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
However, I also want to show an alert view if the app was completely closed and the user wants to open the app via the push notifications.
How can I achieve this?
Thanks
When application launches as a result of a push notification, in other words, when the app was not running, you need to handle that case in [AppDelegate application:didFinishLaunchingWithOptions:] method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:notification[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
If the app is in background or foreground (active) state, you need to handle that case in [AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] method.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Push Notification" message:userInfo[#"aps"][#"alert"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else if (application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) {
// Do something else rather than showing an alert view, because it won't be displayed.
}
}
For more information, you can take a look at Local and Remote Notification Programming Guide from Apple.
I want to display notification popup when app is in foreground state, with alertbody as per code snippets.
It is completely working when app is in background state.
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification == nil)
return;
NSDate *dt = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate date]];
notification.fireDate = dt;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"After 10Secs...";
notification.alertAction = #"View";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
application:didreceiveLocalNotification method in your app delegate if you want to see the nofication while your app is in the foreground:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
//simply show a alert,but the standard one will not show up by itself
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
Hi Im currently developing an app where i have push notifications activated. I use parse.com. I have got it working so far that i can send a notification and the device receives it and i also get a badge on the app. And i have set an AlertView when you enter the app from the notification. But i dont know how to display the text of the push notification in the UIAlertView. And i also want the badge to disappear when you've viewed the message. Here is the code Im using:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateInactive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
else if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
{
NSDictionary * pushDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Text" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
To handle the Push Notification and show an Alert View with its info try in appDelegate:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
I am using following code,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//code here to handle call
//[[UIApplication sharedApplication] openURL:
// [NSURL URLWithString:#"tel:1-408-555-5555"]];
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:#"Teshjhjkhhjkhjkhjkhkjhkhkhkjhjkhjkkkjhjhhjhjkjt" message:#"Test" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert1 show];
[alert1 release];
}
but when application is open I can see the alert, but I want this alert when I press the view button in push message.
Try to implement in this format:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification"
message:[NSString stringWithFormat:#"The application received this remote notification while it was running:\n%#",
[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}