Make a notification alert style - iphone

I am making an application where for it to work properly, the notifications must be set to alert.
I have done my research, and I don't think this is possible, but if it is how?
If it isn't possible, how do I set it so that if the user taps that it is ok if the app sends local notifications, it would set it to alert.
On the xcode simulator how could I make the notification an alert.
How could I Detect if notifications were on and set to alert (if thats possible)

First register for the UILocalNotification and when the notification comes, its delegate method is called,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0)
In this method you can pass the info or do whatever you want to do.
You can use UILocalNotification methods to schedule and cancel your notifications such as
- (void)presentLocalNotificationNow:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
- (void)scheduleLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0); // copies notification
- (void)cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
- (void)cancelAllLocalNotifications NS_AVAILABLE_IOS(4_0);

look at this answer
https://stackoverflow.com/a/9137501/1305001
And
https://stackoverflow.com/a/8259743/1305001
It is now user's choice to choose alert style not a programmer.
You can change your alert style from Device->Settings->Notification Center->Phone->Alert Style to :---
None
Banners
Alerts

What notifications are you talking about, a timed notification of your own?
1. If so, all you would need to do is show a [UIAlertView][1] when your notification is supposed to appear.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hey"
message:#"This is a message"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Okay", nil];
[alertView show];
[alertView release];
2. If the notification may show when the app is not launched, you can use local notifications as you mentioned with UILocalNotification . When the local notification is fired an alert will appear whether the user is in your app or not.
3. For a simulator, same as #1 if I understand what you are saying.
4. You could store the local notifications or some indicator of them in an array and refer to the array to check for pending local notifications.

Related

applicationWillTerminate problem

I want to show the UIAlertView when user Click the Iphone Home button( means Close the application)
I have done these Code
- (void)applicationWillTerminate:(UIApplication *)application
{
NSString *errorString =#"Testing";
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
But UIAlertView is not Showing. please help
Thanks in Advance
The user aims to close your app when he/she presses the home button. Apple suggest to let him/her to that. What I want to say: Don't do that. I think it is even not possible.
Look at Problem with applicationShouldTerminate on iPhone:
The alert view is never shown because
the 'show' method does not block, and
therefore, the end of
'applicationWillTerminate' is reached
immediately after you create the
alert view and try to show it. I
believe this is by design. You can't
really begin asynchronous operations
in 'applicationWillTerminate'.
applicationWillTerminate: may not be called in the newer version of iOS when pressing the Home button because the app could be only entering the background mode, not actually terminating.
Even if it is actually called (either the app is really terminated, or you're moving the code to applicationWillEnterForeground:), showing the alert is useless because the alert is associated with the active app, and your app has gone inactive by the time the alert is shown! So what happened really is, the alert is gone when the user press the home button, and when they resume your app, they see a mysterious alert popping up.
Don't ask if the user wants to quit your app. This isn't the norm in iOS. Instead, save all states in applicationDidEnterBackground:, and restore them in applicationWillEnterForeground: and application:didFinishLaunchingWithOptions:, making the user feel as if the app has never been terminated.
You might not want to use applicationWillTerminate but rather applicationWillResignActive. Check older posts like this one for more info.
You can't show a uialertview when application receive SIGKILL(Exit) Command.You can call any file or background functions in applicationWillTerminate - To do that you need to set a key in your plist.
UIApplicationExitsOnSuspend - Boolean - YES.

Push Notifications without alert

Can I get push notifications without alert view?
The idea in what service for game will send notifications as response for change game state (may be game state will store in database), if this is impossible, what you can suggest about how me send new game state to each connected game client as response of changing game state.
Game will be developing for iPad.
Thanks,
Roman
For me #Ajay answer is not correct (sorry).
With push notification you can choose between three user options: text message (alerts), sounds and badges. Every push notification can contain one or more of these options, so you can send for example a notification with sound and badge, but without message and in this case any alert is shown.
Note that you can even pass hidden options in a private dictionary to your application.
Use content-available property:
The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
Why not!
You can send a hidden push notification without any alert, banner or sound.
PHP CODE
without a text:
$payload['aps'] = array('badge'=> 0, 'sound' => 'default');
Without text and sound
$payload['aps'] = array('badge'=> 0);
and on your ios end you can make a condition for this:
//Receiving a Push Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSMutableDictionary *pushNotiFicationDict = [userInfo objectForKey:#"aps"];
NSLog(#":%#", pushNotiFicationDict);
if([pushNotiFicationDict objectForKey:#"alert"])
{
// when push notification has text
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"MESSAGE" message:[pushNotiFicationDict objectForKey:#"alert"]
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles:nil];
[alert show];
}
else
{
// when push notification does not have text
}
}
But i think you can not perform any action if application is not running or running in background.
No, a push notification will always display a notification as it requires user consent to wake up or launch your application. However if your Application is in the foreground and running, the push notification will not appear and your app can handle the message that the push notification has. All of the preceding applies to local notifications aswhile.
I don't know what you mean by game state. But just have your app listen in on a script on your server which will pass information to your app. Edit: Or like I said above if your app is open in the foreground, push notifications won't appear on screen and you can send information that way. However if you want to do it in the background its not possible no matter what unless you are truly multitasking (GPS, VOIP, Music) or you have user consent through push notification.
You can handle notifications in app and store data in DB, or instead show a message but when app is not running NO.
I agree with #MacTeo. You can delivery a push notification with a payload that contains only one attribute (Sound, Alert (or message) and badge.
With your requirement, keep track of the users device on the server when they open and close the app (foreground and background). If something happens then you can check to see the device's state on the server. You could then decide on how to construct a notification (if any is necessary) base on the last reported state of the device.
Just spitballing:
You could, if you wanted to (however, it isn't what the APNs is designed to do), deliver a push notification with only a badge count = 0 and pass some extra dictionary data with it. That would not alert the user and could be handled in the app if it was in the foreground.

How to Prevent Multiple UIAlertView Alerts from Popping Up at Once?

My tabbar application has three tabs, each with its own navigational structure. Several of the views throughout my app load data via web service calls. These views register this notification in order to know when the app becomes active, so that they can reload the data:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadView) name:UIApplicationDidBecomeActiveNotification
object:NO];
When the app becomes active, these views all try to reload their data; however, if there is no Internet connection, the errors are caught and a UIAlert is shown to the user. The problem is that if 3 of these views are trying to reload data, 3 alert messages pop up.
How do I prevent multiple alerts from popping up to the user?
I appreciate all your thoughts, ideas, and suggestions!!
Thanks!
Brad
Edit: I tried putting this in my appDelegate, but even using this method, I seem to get multiple popups.
-(void)displayAlertWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:#"Cancel" otherButtonTitles:#"Retry",nil];
[alert show];
[alert release];
}
Keep track of whether a alert is currently being displayed (or has been dismissed recently). There really isn't another way.
If you stick the functionality in your app delegate, then you can just do something like [(MyAppDelegate*)[UIApplication sharedApplication].delegate displayNetworkFailureDialog].
EDIT: Note that some people may frown on sticking random global cruft in your app delegate...

Close UIAlertView and Replace with Another

I'm running an application that requires LocationServices be enabled. I'm checking if they are by making a call to the service and catching the error. In the error case, I want to pop up an alertview notifying the user to activate location services. I have another AlertView open already when this test happens. I want to close that one and give the user the dialog box I mentioned previously.
Currently, I have
case kCLErrorDenied: // CL access has been denied (eg, user declined location use)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"NOTICE"
message:#"Sorry, this application needs your location. Please select 'Allow' when asked to use your current location. You don't need to be on or near the trail."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"EXIT"];
[alert show];
[alert release];
//exit(0);
break;
This causes the app to just exit. I had an NSLog output in there so I know it gets to this case.
here you are specifying delegate:self, then it searches for alert handlers declared at UIAlertViewDelegate and when it doesn't find it crashes.
So you should define
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
in your class.
Also you can implement other methods of UIAlertViewDelegate which will help you in your required task.
You need to keep track of the previous alert with an instance variable, and call the method to dismiss that previous dialog before showing the new one. You also need a delegate handler for the alert.

iphone local notification in simulator

I just downloaded xcode and trying to make local notification example. The question is if local notification works in simulator?
thank you
Yes, local notifications work with the simulator. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if you want to see the notification while your app is in the foreground:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
Otherwise, make sure you schedule your notification for some time in the future, then close the application, in order to see the Apple sample work:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = #"Alert!";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
It's easy to think you're not implementing the test code correctly, and you just aren't handling the event while the app is running.
Another gotcha you might find, for anyone who stumbles on this older question: iOS 8 introduced new notification permissions; and your app has to explicitly ask for them.
In your AppDeligate.m:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register local notifications
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
//the rest of your normal code
return YES;
}
If you don't, your notification will never fire, and you'll get a wonderful message like this in your logs:
"Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"
local notifications work on simulator, push notifications do not
To test local notifications in iPhone simulator, follow these steps:
As simulator time is exactly the one in your machine, change the time of your machine to the 1-minute previous of your desired time (when you are expecting your local notification to fire)
Restart simulator(this is awkward, but sometimes it seems iPhone simulator fails to get updated time instantly)
Run the simulator once again(maybe by running your app from xcode, in which case you must press the home button to send your app to background). Once the time is reached you should see the notification
These steps helped me always to get successful local notifications.
App Settings Notification Section not showing
I ran into this issue as well. What seems to happen is the following,
Your app runs / is installed on simulator (might have to reset the simulator state of your app, see below).
Your app has not asked for permission to show notifications.
Therefore your app settings (Settings > App Name) won't show the notification section,
You have to request notifications in the app (using UNUserNotificationCenter.current().requestAuthorization etc).
Then your app will have a section for notifications (even if you deny the allow notification prompt)!
Resetting App State
After this I launched the app again and the notification setting still showed (and showed as Off since I denied permission).
To reset this and reproduce the original problem I had to do the following,
Enable simctl XCode command line utility (See How do I fix the xcrun unable to find simctl error? )
Run in terminal xcrun simctl uninstall booted com.app.bundlename (see How to run a fresh install of the application every time unit tests are run?)