Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 908
Satyam Avatar asked Nov 01 '11 10:11

Satyam


People also ask

Why are my push notifications not showing up on iPhone?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

Do push notifications work when app is open?

You have to put in the notification message title, icon, and content. You can send these messages using the Firebase console UI. In this way, a notification will be shown when the app is running in the background. Data Message: The app should handle these messages.

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.


2 Answers

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   } } 
like image 77
Jakob W Avatar answered Sep 17 '22 17:09

Jakob W


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...)

Example

like image 25
Aviel Gross Avatar answered Sep 21 '22 17:09

Aviel Gross