When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whether they want to view the Notification's contents or not. It just launches, and sits there.
The Push Notifications do work perfectly when the App is running - either as the Active app or while in the background - but nothing works correctly when the app is not running.
I tried logging-out the launchOptions
NSDictionary in application: didFinishLaunchingWithOptions:
to see what load its bringing - but it comes up as "(null)". So It basically contains nothing - which doesn't make sense cause shouldn't it contain the Notification's load?
Anybody have any ideas how to make Push Notifications work when they arrive while the App was NOT running?
EDIT: here's the code I'm using in application: didReceiveRemoteNotification
just to see what's what:
if (UIApplicationStateBackground) { NSLog(@"==========================="); NSLog(@"App was in BACKGROUND..."); } else if (UIApplicationStateActive == TRUE) { NSLog(@"==========================="); NSLog(@"App was ACTIVE"); } else { [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99]; UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM" message:@"app was INACTIVE" delegate:self cancelButtonTitle:@"a-ha!" otherButtonTitles:nil]; [BOOM show]; NSLog(@"App was NOT ACTIVE"); }
So this is supposed to take care of all the application's states - but its not. Push Notifications are only working when the app is running - either in the background or in the foreground...
================================================
UPDATE/EDIT#2: as per "@dianz" suggestion (below,) I modified the code of my application: didFinishLaunchingWithOptions
to include the following:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (localNotif) { NSString *json = [localNotif valueForKey:@"data"]; UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions" message:json delegate:self cancelButtonTitle:@"cool" otherButtonTitles:nil]; [bam show]; }
This does make the AlertView box appear, but there seems to be no payload: the title of the AlertView shows up ("appDidFinishWithOptions"), but the json NSString comes up EMPTY... Will keep tweaking...
======================
EDIT #3 - its now working almost 100%
So, in didFinishLaunchingWithOptions
:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (localNotif) { // Grab the pushKey: pushKey = [localNotif valueForKey:@"pushKey"]; // "pushKey" is an NSString declared globally // The "pushKey" in the "valueForKey" statement is part of my custom JSON Push Notification pay-load. // The value of pushKey will always be a String, which will be the name of the // screen I want the App to navigate to. So when a Notification arrives, I go // through an IF statement and say "if pushKey='specials' then push the // specialsViewController on, etc. // Here, I parse the "alert" portion of my JSON Push-Notification: NSDictionary *tempDict = [localNotif valueForKey:@"aps"]; NSString *pushMessage = [tempDict valueForKey:@"alert"]; // Finally, ask user if they wanna see the Push Notification or Cancel it: UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(from appDidFinishWithOptions)" message:pushMessage delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"View Info", nil]; [bam show]; }
I next implement the alertView: clickedButtonAtIndex
method to see what the user chose on the AlertView and proceed accordingly.
This, along with the didReceiveRemoteNotification
logic works perfectly.
HOWEVER... when the app is NOT running, and I send it a Push Notification, if I DON'T click on the Push Notification alert as soon as it arrives and instead wait for it to fade out (which happens after like 3-4 seconds), and then I click on the App's icon - which now has a BADGE of 1 on it - the app launches, but I don't get the Push Notification alert at all when it launches. It just sits there.
Guess I need to figure that permutation next...
Today, both mobile push notifications are also supported on Android devices. Messages are sent from a mobile app to a user's phone screen. Before viewing these alerts from companies, a user must have downloaded their mobile app and opted into notifications.
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.
Yes. It will. When you click on that. Yeah its ok but being push notification is silent,you are not able to view any alert and cant click.
When your app is not running or killed and you tap on push notification this function will trigger;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
so you should handle it like this,
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (localNotif) { NSString *json = [localNotif valueForKey:@"data"]; // Parse your string to dictionary }
Better be if you override didReceiveRemoteNotification
Here you go
NSDictionary * pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if(pushNotificationPayload) { [self application:application didReceiveRemoteNotification:pushNotificationPayload]; }
This will again fire the didReceiveRemoteNotification
method and your push notification
code will execute as same as it runs during application run time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With