如何在objective c中的didFinishLaunchingWithOption应用程序方法中检测远程通知



当应用处于非后台模式、非活动模式和应用完全关闭时。如何检测是他们的任何通知使用应用程序的委托"didFinishLaunchingWithOption"方法。我已经搜索了很多,但没有得到任何东西。请帮忙。

谢谢

下面的方法是用于通知的

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

您可以在didFinishLaunchingWithOption方法中执行此操作

 let launchedFromRemoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil

您可以通过以下方式在didFinishLaunchingWithOption中获得通知:

(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");
   }
}

试试这个,它可能对你有帮助。

如果应用程序被终止并重新启动,只有当用户在通知托盘中点击远程通知时,您才能检测到远程通知

你可以在didFinishLaunchingWithOptions方法中检测它
- (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
   }
}

相关内容

  • 没有找到相关文章

最新更新