Reputation: 1828
I'm using apple push notification service in my project.
Please follow the 2 ways of opening the app and handling this push notifications. In the second scenario I do not know how to handle it. Do you know how?
The push notification arrived to my device,
Scenario 1:
I clicked on the push notification.
The - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
function AppDelegate.m
file catches this function.
Scenario 2:
I normally opened the device (by clicking on the app)
How can I handle the push notification?
Upvotes: 3
Views: 529
Reputation: 393771
The other answers show how to get the notification data when the user taps the notification.
The difference between the two nethods shown is that one is called when app is already running, either in foreground or background, while the other is called when app is not running at all.
On your second case, when the user doesn't tap the notification, the notification data isn't passed to the app when you open it with the launch Icon.
Upvotes: 3
Reputation: 18865
First scenario:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog (@"APNS: notification received: %@", userInfo);
NSString *message = nil;
id alert = [userInfo objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:@"alert"];
}
if (message)
{
if (![message isEqualToString:@""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"notification"
message: message
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
Second scenario:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog (@"LAUNCH OPTIONS: %@",launchOptions);
id remoteNotificationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotificationValue)
{
NSString *message = nil;
id alert = [remoteNotificationValue objectForKey:@"aps"];
if ([alert isKindOfClass:[NSString class]])
{
message = alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
message = [alert objectForKey:@"alert"];
}
if (message)
{
if (![message isEqualToString:@""])
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"notification"
message: message
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
}
....
Of course you might want to make a special method that handles notifications and is called from both scenarios (with NSDictionary
* parameter) so your code would be more readable. Sometimes APNS notifications are useful also when app is running - empty notification (with no payload) might be used to trigger the data synchronization with server to avoid polling for example.
Upvotes: 2
Reputation: 14251
You can handle that like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Checking if app was launched from the notification
if (launchOptions != nil) {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil){
// Read dictionary and do something since the app
// was launched from the notification.
}
}
Here is an example of what the dictionary object contains
NSString *message = @"";
NSString *badge = @"";
NSString *sound = @"";
if([dictionary objectForKey:@"alert"]) {
message = [dictionary objectForKey:@"alert"];
}
if([dictionary objectForKey:@"badge"]) {
badge = [dictionary objectForKey:@"badge"];
}
if([dictionary objectForKey:@"sound"]) {
sound = [dictionary objectForKey:@"sound"];
}
Hope it helps!
Upvotes: 0
Reputation: 843
You can get the arrived notifications when the app starts with the following code (e.g: in application:didFinishLaunchingWithOptions):
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
Here is a more thorough explanation: How to manage notification when users click on badge
Upvotes: 0