6

我在我的应用程序中设置了推送通知。我有方法:

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:     (NSDictionary *)userInfo
 {
      if()
      {
           //app is in foreground to get here
      }
      else if()
      {
           //app is in background and then the notification is clicked, to get here
      }
 }

我需要区分应用程序外的通知触摸,以及简单地在应用程序中接收通知。有什么帮助吗?

4

4 回答 4

7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app was already in the foreground
    else
        // app was just brought from background to foreground
    ...
}
于 2013-07-12T09:55:42.243 回答
3

使用以下方法在前台和后台接收推送通知。在后台,您可以显示默认警报/横幅,但在前台,您不能显示警报/横幅,但您可以通过 UIAlertView 管理它。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateBackground | application.applicationState == UIApplicationStateInactive )
    {
        // Application is in Background
    }
    else
    {
        // Application is in Foreground
    }
}

它工作正常...!!!

于 2013-07-12T10:50:59.020 回答
2
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        applicationIsActive = NO;
    }
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
       applicationIsActive = YES;
     }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (applicationIsActive)
{
 }
  else 
 {
  }
}
于 2013-07-12T10:00:12.910 回答
1

最好用UNUserNotificationCenter.

UNUserNotificationCenter.current().delegate = self
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // a notification arrived while the app was in the foreground and has a value for badge, alert, or sound
    // if the notification also has content-available=1 then didReceiveRemoteNotification will also be called.         

    // we can have iOS to display the alert normally  
    completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // the user took an action (i.e. tapped) on a notification 
    // unless you support custom notification action that run in the background the app is now in the foreground. The tap may have caused the app to launch OR brought it to the foreground OR the app may have already been in the foreground
    completionHandler()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // a notification arrived with content-available = 1
    // the application may be in the background or foreground
    // note: if the app is in the foreground AND the notification also has a value for badge, alert, or sound then the userNotificationCenter(willPresent) method will also be called
    completionHandler(.newData)
}
于 2018-03-19T13:41:12.833 回答