Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification data retrieving

I want to get data from push notification message. I successfully get the data when app is on foreground and in background. but I am unable to get data when app is quit and user press view button on push notification. I write the code in application did finish launching. This code cause the app crash when pressing on View button of push notification message. If I comment the code then app doesn't crash. Kindly help me to fetch data from push notification when app is quit and user press view button on push notification. I'll really appreciate that.

if(launchOptions != nil){
        NSDictionary *tmpDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (tmpDic!=nil) {
            pushedMessage=[NSString stringWithFormat:@"%@",[[tmpDic objectForKey:@"aps"] objectForKey:@"alert"]];
            pushedCountry=[NSString stringWithFormat:@"%@",[tmpDic objectForKey:@"country"]];
            [self saveToDatabase];
        }
    }
like image 849
Idrees Ashraf Avatar asked Nov 04 '22 23:11

Idrees Ashraf


2 Answers

Please try this...

Add this code to appdelegate.m => didFinishLaunchingWithOptions

if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]) {

    [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];

}
like image 172
Ajay Gabani Avatar answered Nov 15 '22 05:11

Ajay Gabani


From ios7 we have the below delegate method to handle the push notification when the app is in back ground or not running

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler 
like image 44
Rahul K Rajan Avatar answered Nov 15 '22 06:11

Rahul K Rajan