How to detect Remote notification in didFinishLaunchingWithOption application method in objective c?

后端 未结 4 1435
予麋鹿
予麋鹿 2021-01-28 22:34

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 \"didFinishLaunchingWi

相关标签:
4条回答
  • 2021-01-28 22:56

    You can do this in the didFinishLaunchingWithOption method

     let launchedFromRemoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
    
    0 讨论(0)
  • 2021-01-28 23:11

    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 {
      }
    
    0 讨论(0)
  • 2021-01-28 23:14

    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.

    0 讨论(0)
  • 2021-01-28 23:19

    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
       }
    }
    
    0 讨论(0)
提交回复
热议问题