iOS - Push notification alert is not shown when the app is running - ios

I've integrated push notifications in my app. Users will receive push notification to join a group. When the user clicks Join, I've to handle something in the code. And so I'm implementing:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
This is working fine when the app is not running.
When the app is running, I don't see any UIAlertView. How can make my app show the push notification alert so that user can still decide whether to join or not?

I used code like this in my application delegate to mimic the notification alert when the app was active. You should implement the appropriate UIAlertViewDelegate protocol method(s) to handle what happen when the user taps either of the buttons.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
[alertView release];
} else {
//Do stuff that you would do if the application was not active
}
}

For anyone might be interested, I ended up creating a custom view that looks like the system push banner on the top but adds a close button (small blue X) and an option to tap the message for custom action. It also supports the case of more than one notification arrived before the user had time to read/dismiss the old ones (With no limit to how many can pile up...)
Link to GitHub: AGPushNote
The usage is basically on-liner:
[AGPushNoteView showWithNotificationMessage:#"John Doe sent you a message!"];
And it looks like this on iOS7 (iOS6 have an iOS6 look and feel...)

You have to show the alert yourself if you want to. This is intentional behavior as documented here http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html below Listing 2-6

only this function will be invoked and you have to explicitly show the alert on that case no notification will come if app is running in which you have implement the notification.Put the break point there and handle the notification call when function called and show your customized alert there.

For showing alert view while running application you have to use
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
and by accessing the userInfo variable

The app will still receive the -application:didReceiveRemoteNotification message in your App Delegate, but you'll have to act on the message yourself (i.e. the alert isn't displayed by default).
The userInfo parameter contains an object with the key notificationType, which you can use to identify the push message.

Here is a version which support UIAlertController
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if (state == UIApplicationStateActive) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:notification.alertTitle
message:notification.alertBody
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self.navigationController presentViewController:alert animated:YES completion:nil];
}
}
** Please note my app uses self.navigationController in App Delegate, just hook on to any ViewController to present ( show ) the Alert **

Related

cordova iOS notification don't work in foreground

I'm using Cordova and the plugin phonegap-plugin-push,
when my app is in foreground I don't see the notification alert.
What I have to do?
(I know the way to do it on Android platform (setting "forceShow": true in the method init of PushNotification) but this works only for Android)
Thank you in advance for your replies.
If u don't want to touch ios native things, then you can display your own custmize html/css Banner (like iOS push Baner) in following method :
push.on('notification', function(data) {
If(platform == "iOS" && data.additionalData.foreground){
// Show your Baner here
// U can also define call back on clicking of that banner
//Like navigation to respective page or close that banner
});
Denise,
Notification doesn't work in the foreground. It only works in the background. Do handle it in the foreground you need to implement the logic in the delegate method.
Below is the Objective C code snippet to show the notification in the foreground.
-(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:#"Your App name received this notification while it was running:\n%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
It will help you for your problem.
In iOS if your app is in foreground, push notification alert will not show. To achieve it, you should write code for alert in push notification receive delegate if your application is in foreground. To know application state in iOS this is the code.
+(BOOL) runningInForeground
{
UIApplicationState state = [UIApplication sharedApplication].applicationState;
return state == UIApplicationStateActive;
}

display Push notification text in a UIAlert View

I am using Parse to deliver push notifications to my app. What i would like is, when the user swipes the notification from the lock screen, or taps it from the home screen, that it opens up the app and displays the text of the Push notification in a UIAlert View.
I imagine that it would slot into the didfinishlaunchingwithoptions method of the app delegate, but i really have not got a clue on how to extract the text of the alert.
Any advice would be greatly appreciated.
So below is my code to display the alert - which is working, but it displays the full JSON message - and when i try to use the objectForKey:#"alert statement to try and extract just the "alert" part of the push notification, nothing is displayed in the message section of the alert.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
pushText = [userInfo objectForKey:#"alert"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"News", "")
message:pushText
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
You can put this code to the didFinishLaunchingWithOptions section and then display the pushText variable in a UIAlertView.
NSDictionary *notifKey = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (notifKey)
{
pushText = [notifKey objectForKey:#"alert"];
}

Handle local notification only when the user clicks on it

I'm actually working on an iOS application in which I need to handle notifications.
I'm currently using local notifications, and I need to redirect to different viewControllers when I click on a notification.
My problem is that for now the redirection is made every time I receive a notification and the application does not wait for me to click on it.
I tried many solutions and I always have the same problem.
So I'm wondering : Is it possible to redirect to specific views only when the user click on the notification and not when it appears ?
Thanks you in advance for your help.
You can try as below. I have also written code, which may help you.
When application is in Active state : you may show alert and ask user for redirect to different screen or not.
When application is in Inactive state : you may redirect user
directly to required screen because they coming in app via tap on
notification.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"View", nil];
[alert show];
}
if (state == UIApplicationStateInactive)
{
// Redirect User to your required screen.
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
// Redirect User to your required screen.
}
}
You can judge the app state when you receive local notification using [[UIApplication sharedApplication] applicationState]
If the app is in active state, you can just show a UIAlertView indicating user receiving the local notification.So user click on the alertView,you can redirect the viewController.
I hope my answer may help you.

How to show an alertview on receiving push notifications when app is not launched?

I am new to iOS development. I want to show an alertview on receiving a push notification while app is inactive.
Apple says that push notification can be presented in form of alert message or they can badge the application icon.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html
I am using PushSharp to send push notifications. But notifications are seen in notification center only. What should I do to show it on alertview?
I know we can write a code to show alertview like
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"test title" message:#"test message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
}
But this code is executed after I tap notification and app launches. I want to show alertview as soon as device receives a push notification.
One more question, does Apple support any way to execute a code on receiving push notification though app is not launched?
Any ideas?
Not Possible with the UIAlertView . You can go through the UILocalNotification to achieve your task .
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
notifaication = [[UILocalNotification alloc] init];
notifaication.timeZone = [NSTimeZone systemTimeZone];
notifaication.alertBody = // Set the Body
notifaication.alertAction =// #"Show me";
backupAlarm.soundName = UILocalNotificationDefaultSoundName;
}
For More info over_Here

Is it possible to let remote notifications show in the notification center even when an app is running?

Is it possible to let a remote iOS5 notification show in the notification center even if my app is running, something like "I don't care about the remote notification at the moment even though I am running, let it show in the notification bar just as if I wasn't running"?
You show the UIAlertView to the user, instead of that.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo :%#",userInfo);
NSString* msg = [userInfo valueForKey:#"aps"];
if (self._VCObj.isViewLoaded && self._VCObj.view.window) {
// viewController is visible don't show.
}
else { // viewController is not visible
[[[UIAlertView alloc]initWithTitle:#"Title" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil] show];
}
}
}
Tutorial
This is not possible. The only workaround I can think of is store notification somehow in app, and then on applicationWillTerminate(or some other method) schedule same notification as a LocalNotification and made it appear after second or two. This however could annoy me as a user if I get a notification right after I close some app :)

Resources