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

徘徊边缘 提交于 2019-12-20 05:47:24

问题


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


回答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 {
  }



回答2:


You can do this in the didFinishLaunchingWithOption method

 let launchedFromRemoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil



回答3:


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.




回答4:


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
   }
}


来源:https://stackoverflow.com/questions/38007027/how-to-detect-remote-notification-in-didfinishlaunchingwithoption-application-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!