1

i implemented the Remote Notifications in my application! if my App is in Background and a Push Message was send to my Device, i react with this method:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}

This is working great when App is in Foreground or in Background State! But what if my App is not running at all?! Can´t i react to Push Messages when the app is not running?I mean WhatsApp can do this, right?!

davidOhara
  • 978
  • 4
  • 16
  • 38
  • possible duplicate of [Detect if the app was launched/opened from a push notification](http://stackoverflow.com/questions/16393673/detect-if-the-app-was-launched-opened-from-a-push-notification) – joao Feb 10 '14 at 13:46
  • Thats not the same Question at all.... – davidOhara Feb 10 '14 at 14:01
  • Isn't your problem handling the push notification when the app was launched from the notification? It is covered there, but I might have misunderstood your problem... – joao Feb 10 '14 at 14:06
  • Nah...i just want to send a silent push message and handle it in the background! But if my App is not running / terminated the given methods are not be called – davidOhara Feb 10 '14 at 14:08
  • check this for app background running info: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html – joao Feb 10 '14 at 14:14
  • possible duplicate of [iOS 7 Background Fetch by Push Notification - Will iOS launch my app if it is not in the background?](http://stackoverflow.com/questions/19068762/ios-7-background-fetch-by-push-notification-will-ios-launch-my-app-if-it-is-no) – Eran Feb 19 '14 at 02:41

1 Answers1

1

If user clicks on push notification from notification center you will have information in launchOptions with the push notification content and you can use below code to check if application was launched clicking push notification or it was there as well,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];        
    NSLog(@"LaunchOptions->%@",launchOptions);
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self performNotificationAction:userInfo];
    }

    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//    NSLog(@"userInfo->%@",userInfo);
    [self performNotificationAction:userInfo];
}

-(void)performNotificationAction:(NSDictionary*)userInfo{
    //Do the stuf whatever you want.
    //i.e. fetch the message or whatever extra information sent in push notification
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Janak Nirmal
  • 22,510
  • 18
  • 60
  • 95
  • i don´t want the user to click on anything...i just want to send a silent push message and handle it in the background! But if my App is not running / terminated the given methods are not be called...But how does WhatsApp it?! – davidOhara Feb 10 '14 at 14:05
  • Than probably you are looking this [application:didReceiveRemoteNotification:fetchCompletionHandler](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:) – Janak Nirmal Feb 10 '14 at 14:07
  • In iOS7 they have added a new method which can invoke your application, but deployment target would be iOS7 only. – Janak Nirmal Feb 10 '14 at 14:08
  • Yee i know that! But it does´t work! As you can see in my Question, i already implemented - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler! But this method won´t be called if app is terminated – davidOhara Feb 10 '14 at 14:09
  • It should be called as in documentation they have stated `Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method.` – Janak Nirmal Feb 10 '14 at 14:11
  • And perhaps may be [looking at this question](http://stackoverflow.com/questions/18856204/applicationdidreceiveremotenotificationfetchcompletionhandler-not-called) may help – Janak Nirmal Feb 10 '14 at 14:12
  • Yes exactly...i read it already in the docs...The Method should be called even if the is in not running state! But it doesn´t get called...Thats the weird thing... – davidOhara Feb 10 '14 at 14:17
  • @davidOhara Even I am struggling with the same kind of issue in my app. Did you got any solution for it? – Saurabh Feb 18 '19 at 06:27