How to use NSThread with UILocalNotification to show custom view? - iphone

I am developing an application of Fake incoming call.
For that, i am right now using local notification.
I am getting notification as per the set time but it gives me the pop up, like a by default notification.
Instead this, i have to show the view from either XIB or by codeing.
So, is there any way to set the custom view with localnotification?
Or can i use NSThread to get the above mentioned output?
If anyother way to get the disered output, then please let me know.
Thanks in advance
This is what i am doing to set the notification.
-(void)addNotification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[localNotification setAlertAction:#"Answer"];
[localNotification setAlertBody:[NSString stringWithFormat:#"%#\n\n %#",[txt_CallerName text],[txt_CallerNumber text]]];
[localNotification setHasAction: YES];
[localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system
[alertNotification setHidden:NO];
}

Showing a custom interface on receiving a UILocalNotification is not possible right now. It is possible if you happen to receive the UILocalNotification when your app is in the foreground. But if it is in the foreground, you could show your custom interface regardless.
The way you catch the local notification firing and do something when that happens is by implementing -application:didReceiveLocalNotification: in UIApplicationDelegate. Arguably, it is the answer to your question, but it won't achieve what you're clearly looking for, which is to start your app at a specific point in time by showing custom UI, because it will only happen while the app is already in the foreground.

You can use NSNotificationCenter to throw notification and event occurred just call the notified method and display your custom view.
1.Register the Notification as follows:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(displayCustomView) name:#"EventNotification" object:nil];
2.When event occur notify method as follows:
[[NSNotificationCenter defaultCenter] postNotificationName:#"EventNotification" object:nil];
3.In notified method display your custom view as follows:
-(void)displayCustomView
{
//initialize custom view and display as required.
}

Related

Validate if call was made after telprompt

I want to validate if user tapped on Call button or Cancel button after telprompt
Current code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", [personDetails valueForKey:#"phone"]]];
[[UIApplication sharedApplication] openURL:url];
How can I do so?
First telprompt: is not a documented URL schema and should not be used. Since Apple can change the way it used at any moment.
Second since data is passed back to your app, you will not be able to detect a if call was made. You might be able to detect if you use the CoreTelephony. But getting this to work require your app to run in the background and you might have to misuse some background mode for this which will make Apple reject your app.
Can you explain why you want to detect if there was a call made?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(called:) name:#"UIApplicationSuspendedNotification" object:nil];
-(void)called:(NSNotification *) notification
{
NSLog(#"Tapped Call button");
}
if Call button was tapped then application will be terminated and go into background so just add an observer for the UIApplicationSuspendedNotification notification.
For my case, I used tel:// instead of using telprompt:// and make my own UIAlertView. This way you could detect if the call option is tapped from the UIAlertView's delegate.

how to hide UipickerViewController in applicationWillResignActive?

I my application i want to hide the UIPickerview controller when application enter's in a paused state?
for eg: in my viewcontroller i am using uipickerview,and is placed in a popovercontroller,but i have not dismiss the popovercontroller and the application goes in a paused state.
when user open the application the uipikcer view is displayed which i want to dismiss.
please help me out with this.
Use NSNotificationCenter for hide the UIPickerView when application go in background like bellow...
[[NSNotificationCenter defaultCenter] addObserver:alertView selector:#selector(HidePickerView) name:#"UIApplicationWillResignActiveNotification" object:nil];
and hide the UIPickerView in bellow method..
- (void) HidePickerView {
[yourPickerView setHidden:YES];
}
You need to subscribe for the UIApplicationWillResignActiveNotification notification and when it's fired and your selector is called, you should [self.myPickerView dismissViewControllerAnimated:NO completion:nil];.
Note: -viewWillDisappear: method would not be called when the app is resigning active.

Push notifications ios

If my app is closed , and the iphone receives a push notification for that app, will it receive it and open the app?
Thanks
No if the message is received it will not open the app.
You app wil get launched if the user selects to view the notification.
Thus if the user does not react to the notifications your app will not be launched.
If your app is already running and in the foreground than the app delegate will receive the notification directly.
Yes it will launch if "View" button is clicked and application will call the delegate method.
If clicked on "close" button it will discard the notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
For more info http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 and http://mobiforge.com/developing/story/programming-apple-push-notification-services
YES. http://urbanairship.com/products/push-notifications/
when the notification aries that time you open the application that for you can use the following code.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
// Current date
NSDate *date = [NSDate date];
// Add one second to the current time
NSDate *dateToFire = [date dateByAddingTimeInterval:1];
// Set the fire date/time
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
// Setup alert notification
[localNotification setAlertBody:#"Tap to return to TestApp" ];
[localNotification setAlertAction:#"Open TestApp"];
[localNotification setHasAction:YES];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
And more information regarding this thing you can use Refer the Reference.
May this code helping to you.
Yes, when you receive push - it appears as an alert on the screen on appears in the top (you can choose a way in Settings) as a message which will disappear after some seconds. If you click on push notification - it'll open App. But when you receive push, App will not be opened automatically with a receiving a push. Only after user clicks on push.

UILocalNotification not showing up in Notification Center when app is in background

I have a GPS app that registers to run in the background. I also show a UILocalNotification when I have finished a process. This correctly shows, and if the app is open, then it also appears in Notification Center (swipe down from top). But, if I call the UILocalNotification when my app is in the background, or the screen is locked, I DO get the notification, but it does NOT show up in Notification Center.
I am correctly registering for notifications in my app delegate (iOS 5 bug workaround):
// Register for notifications
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Calling the notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.alertBody = msg;
localNotif.alertAction = NSLocalizedString(#"View", nil);
localNotif.soundName = #"alert.caf";
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
Is this a bug? Why would it show up in Notification Center only when my app is open, even though the notification is shown to the user, and not other times?
Are you also using push notifications? I believe there is no need to call registerForRemoteNotificationTypes: if you are only using local.
Check Here
From the look of the code you posted, it does not look like the code would run in the background. It looks like you are trying to present a Local Notification when an even occurs in the background. You can not do this. Local Notifications are meant to be scheduled while the app is running to go off at a later time when the app is not running, and then they will trigger even when the app is closed.
You need to use Push Notifications. Check out Apple's documentation here.
EDIT: Is your app enabled for the notification center in settings?
have you added the UIBackgroundModes key to your Info.plist file ?
You need to have the UIBackgroundModes Key set to location.

iPhone SDK - Notification firing multiple times, and pushing multiple views onto the stack

Ok, so this is actually solved - but I don't understand why what I did worked.
My issue was that sending a notification once would cause one event to fire multiple times. I ended up with several unwanted views on the stack. in a nut shell:
the user presses a button in the toolbar, a notification is sent from the delegate
mapItem = [[UIBarButtonItem alloc] initWithImage:mapImage style: UIBarButtonItemStylePlain target:self action:#selector(mapButtonPressed:)];
-(void)mapButtonPressed:(id)sender{
NSLog(#"Map Button Pressed");
[[NSNotificationCenter defaultCenter] postNotificationName:#"mapButtonPressed" object:nil ] ;
}
this fires a function in the current view to push a map view onto the stack.
-(void)openListMap:(NSNotification *)aNotification {
mapViewController = [[MapViewController alloc] initWithNibName:#"MapViewController" bundle:nil];
NSLog(#"Map Created");
mapViewController.searchLocation = searchLocation;
if(givenLocationType == #"input"){
mapViewController.inputLocationText = inputLocationText;
}
mapViewController.givenLocationType = givenLocationType;
CultureNOWAppDelegate *delegate =
[[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:mapViewController
animated:YES];
}
This works now, I changed the last line from:
CultureNOWAppDelegate *delegate =
[[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:mapViewController
animated:YES];
to:
[self.navigationController pushViewController:mapViewController animated:YES];
The result is that, although the openListMap function still fires multiple times (you can see in the console, the log output shows "Map Created" for each time the view has appeared since the app started) it only pushed the latest mapView onto the stack.
But why? Why was it firing multiple times in the first place, and why has it stopped by exchanging two piece of code which, for all intents and purposes, are the same?
Thanks for any thoughts.
The fact that it is firing 2 times warns me that your "fix" is not really a fix - it is a behavior of Apple's API that happens to do what you want.
I had a similar issue with an app where a notification was inexplicably being fired twice. What I realized later was that the notification is only called once - but that inside NSNotificationCenter, there is nothing to stop you from registering TWO observers for the exact same event for the exact same selector callback.
This happened to us because we added observers in viewDidLoad, but never removed the observers in viewDidUnload. Then, when the user's phone had low memory (which, thanks Apple, happens a lot in iOS4+), the views get flushed, and you end up with 2 observers when viewDidLoad gets called a second time.
That may not be your exact problem - but I would like to hear about where you are registering for observer notifications.