Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crash when launch with a push notification

I have integrated push notifications for my app. To catch the notifications I used this delegate.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfoin my app delegate.

So when the app is running in background if notification came and when I click on it this delegate fire. If the app is not running even in the background, If clicked on ntification then it fires

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Sofar it worked well. Then I wanted to catch notification in the background. So I found

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

this works for it.

So I just change my previous didReceive method into this new didReceive method. Now my problem is when app launch with push notification (if app doesnt run in background or forground and when click on the notification when comes) my app crashes. Even I cant debug and catch this situation.

What is the difference between these 2 delegates. Does my 2nd delegate fire when app launch with a notification? Please help me.

like image 317
user1960169 Avatar asked Oct 14 '15 05:10

user1960169


1 Answers

Normally when a notification came then this method executes(when app is active) - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

But if the app is closed or killed by the system then click on the notificatin first calls the "didFinishLaunchingWithOptions" method i thsi method we have to check wether app starts from notification or a fresh start the we can use this code to call the "didReceiveRemoteNotification" method again

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (notification) {
    [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}
like image 188
Parv Bhasker Avatar answered Nov 04 '22 07:11

Parv Bhasker