I\'m able to send push notifications to my IOS device. But when I click on that notification it just opens the app. No message is shown inside the app.
Code used by
Handling Push Notifications when App is NOT running (or Totally Killed)
I'm posting this solution as it worked for me.
Go to your AppDelegate.m file.
Step 1: Write this code inside this function:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
Step 2:
Insert This full code:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
/**
* Dump your code here according to your requirement after receiving push
*/
if (application.applicationState == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
else if(application.applicationState == UIApplicationStateInactive){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
NSString *cancelTitle = @"Close";
NSString *showTitle = @"OK";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
This whole code will work whether app is Active, InActive or Totally Killed. It will give you AlertView for push messages.
I think there is one easy solution.
If you want to show the alert only when you open the application from the notification then maybe you this is your solution
When application is totally killed get notification code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
//opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]);
//write you push handle code here
}
}
}
For more go through this link: Handling Push Notifications when App is Terminated
The best way to handle things like this is to use deep linking within your APN. That will let you embed data that can then be handled within your app and direct the user to a specific event.
Otherwise, you are limited to using the ApplicationDidEnterForeground
method from your app delegate. Just put your alertView code in there and anytime your application is brought into the foreground that will run.
From the apple documentation
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/Introduction.html
"When your app must be launched to receive a notification, UIKit includes the UIApplicationLaunchOptionsLocalNotificationKey or UIApplicationLaunchOptionsRemoteNotificationKey key in the launch options dictionary passed to your app delegate’s application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods. The presence of those keys lets you know that there is notification data waiting to be handled and gives you a chance to configure your app’s interface appropriately. You do not need to handle the notification in these methods, though. After your app is running, UIKit calls other methods of your app delegate, such as the application:didReceiveLocalNotification: method, to give you an opportunity to process the notification data. Which methods are called depends on which methods you implemented and whether the user interacted with the system UI for the message."
So check if your app has been launched due to a notification and if so display the dialog.