0

when app in not in background mode ,inactive mode and app is completely closed. than how to detect is their any notification using application's delegate "didFinishLaunchingWithOption" method. i have searched a lot about it but not get anything. please help .

Thanks

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571

4 Answers4

1

Below methods is used for notifiaction

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
   if (notification)
   {


   }
}


 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[[[deviceToken description]
                      stringByReplacingOccurrencesOfString:@"<"withString:@""]
                     stringByReplacingOccurrencesOfString:@">" withString:@""]
                    stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"Token:%@",token);

}

//app is forground this method will access
 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

}
//need to on teh background fetch option in info plist
//app is background state this below mthod will call while notification receives
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 {
  NSLog(@"Background mode working%@",userInfo);

  if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification when recive preferences and text messages
  {
  }
 }

//handling interactive notification
   - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(nonnull UILocalNotification *)notification completionHandler:(nonnull void (^)())completionHandler {
  }
HariKrishnan.P
  • 1,204
  • 13
  • 23
  • is this background fetch method allow me to fetch notification data when the app has been closed ? (suspended.) – Harry Saggu Jun 24 '16 at 07:36
  • your answer is good i will try this on immediate basis.. but i want know is this method give me notification data on app's suspended state.. – Harry Saggu Jun 24 '16 at 07:42
  • In background is possible to get the notification data. background means when press the home button to close the app. suspended state means when double click the home button then swipe app to close @Harry – HariKrishnan.P Jun 24 '16 at 07:43
  • Thanks sir for your information . your answer is good but not explained .. – Harry Saggu Jun 24 '16 at 07:58
  • Yes, if user click the notification to relaunch. then you get data from "didFinishLaunchingWithOptions"methods. if the user don't click the notification. user click the app to launch the application at time we are not able to get the data. – HariKrishnan.P Jun 24 '16 at 09:15
  • but the possible ways is store the push notification messages in server database and use API to get the data while app launch. – HariKrishnan.P Jun 24 '16 at 09:17
0

You can do this in the didFinishLaunchingWithOption method

 let launchedFromRemoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
Code
  • 6,041
  • 4
  • 35
  • 75
0

You can get Notification in the didFinishLaunchingWithOption in this way :

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

  NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if (notification) {
          NSLog(@"app recieved notification from remote%@",notification);
   }else{
        NSLog(@"app did not recieve notification");
   }
}

Try this it may help you.

Bhumika
  • 876
  • 7
  • 20
0

If the App was terminated and is starting again, you can detect the Remote Notification only if the App is being launched because the user tapped on the remote notification in the notifications tray.

You can detect it in the didFinishLaunchingWithOptions method

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

  NSDictionary *notificationDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if (notificationDict) {
        //Your App received a remote notification
   }else{
        //Your App did not receive a remote notification
   }
}
7vikram7
  • 2,764
  • 1
  • 25
  • 43