Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle push notifications if the application is already running?

How do we handle push notifications if the application is already running ? I want to show an alert if the application is running (instead of a push notification alert). Only if the application is not running, then show a push notification alert.

Also, if I send a payload to APNs, how can I create an alert with a cancel button?

like image 778
Rahul Vyas Avatar asked Oct 12 '09 14:10

Rahul Vyas


People also ask

Do push notifications work when app is open?

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

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.

How do I allow push notifications for an app?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.


1 Answers

You can implement application:didReceiveRemoteNotification:

Here is a possible sample code:

- (void)application:(UIApplication *)application    didReceiveRemoteNotification:(NSDictionary *)userInfo {   NSString *message = nil;   id alert = [userInfo objectForKey:@"alert"];   if ([alert isKindOfClass:[NSString class]]) {     message = alert;   } else if ([alert isKindOfClass:[NSDictionary class]]) {     message = [alert objectForKey:@"body"];   }   if (alert) {     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"                                        message:@"AThe message."  delegate:self                              cancelButtonTitle:@"button 1"                              otherButtonTitles:@"button", nil];     [alertView show];     [alertView release];   } 
like image 151
notnoop Avatar answered Sep 21 '22 18:09

notnoop