App crash when launch with a push notification
Asked Answered
S

1

6

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.

Sirree answered 14/10, 2015 at 5:19 Comment(3)
check once payload you have correctly serilize or notViewfinder
I have several notification types. If the notification type is 12 only I need to do something in background. So I added 'content-avilable key $body['aps'] = array( 'alert' => $message, 'sound' => 'default', 'badge' => 1, 'content-available'=>1 ); If my notification type is 12 content-available become 1 in all other times it 0Sirree
Have a look at this ans, may be helpful https://mcmap.net/q/1918017/-didreceiveremotenotification-fetchcompletionhandler-not-being-called-when-app-is-in-background-and-not-connected-to-xcodeOutgoings
K
1

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];
}
Kuomintang answered 14/10, 2015 at 5:50 Comment(2)
The only thing I did was changing void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo into UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler but this made my app crash when it launch from a notification. What is the reason for it? In didFinishLunch method im redirecting to another view. It works fine without fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler part.Sirree
simple solution to your problem is that just use this method for the notification implementation and handling (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary *)userInfo and put this code in the "didFinishLaunchingWithOptions" method so that if ur app is active the it directly went to the notification mehod but if ur app started by clicking on the notification then again the code [self application:application didReceiveRemoteNotification:(NSDictionary)notification]; send the control to the notification methodKuomintang

© 2022 - 2024 — McMap. All rights reserved.