【问题标题】:How to handle push notification in background in ios 10?如何在 iOS 10 中处理后台推送通知?
【发布时间】:2017-11-27 18:05:41
【问题描述】:

我不在后台处理推送通知。

对于在后台处理推送通知,请执行以下步骤:-

  1. 在功能中 -> 启用远程通知。
  2. 在功能 -> 后台模式 -> 启用远程通知。
  3. 在 didFinishLaunchingWithOptions 中授予 ios 10 的所有权限。
  4. 用于推送通知UNUserNotificationCenter。
  5. App In Foreground 然后推送通知工作正常,并且在方法调用下方:

    userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    

但我的问题是后台应用程序然后不调用任何方法。所以任何人都有想法或解决方案在后台处理 ios 10 的推送通知然后请帮助我。

谢谢。

【问题讨论】:

  • 嗨,我有类似的情况,但需要稍作修改,比如当我收到推送通知时,应用程序应该在后台激活并执行中给出的任务,(如用户的位置更新代码), - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ,但我的应用程序在用户点击通知之前不会激活。那么,当应用程序处于后台时,我将如何完成这项工作?有谁知道这个的解决方案吗?

标签: ios objective-c push-notification ios10


【解决方案1】:

willPresentNotification 在应用处于前台时调用。看看他们的文档

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    // The method will be called on the delegate only if the application is in the foreground.
    // If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
    // The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
    // This decision should be based on whether the information in the notification is otherwise visible to the user.

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)())completionHandler {
    // The method will be called on the delegate when the user responded to the notification by opening the application,
    // dismissing the notification or choosing a UNNotificationAction.
    // The delegate must be set before the application returns from applicationDidFinishLaunching:.

}

尝试签入didReceiveNotificationResponse你会得到你需要的。

ALSO如果需要获取任何数据或任何处理,请在后台模式下启用后台获取并使用以下方法

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{

    completionHandler(UIBackgroundFetchResultNewData);
}

根据应用状态处理 APNS

   if(application.applicationState == UIApplicationStateInactive)
     {
        /* 
        # App is transitioning from background to foreground (user taps notification), do what you need when user taps here!
         */    
    }
    else if(application.applicationState == UIApplicationStateActive)
    {
        /*
         # App is currently active, can update badges count here
       */
    }
    else if(application.applicationState == UIApplicationStateBackground)
    {
        /* # App is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here */
    }

【讨论】:

  • 你好 abhishek,在 ios 10 中调用了 didReceiveRemoteNotification 吗?
  • 你好 abhishek,我尝试了你的解决方案,但它也不起作用,所以请帮助我。
  • 是的,静默通知将被称为 didReceiveRemoteNotification
  • @DivyaPatel 同时启用后台获取并重试
  • 嗨,我有类似的情况,但需要稍作修改,比如当我收到推送通知时,应用程序应该在后台激活并执行中给出的任务,(如用户的位置更新代码), - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ,但我的应用程序在用户点击通知之前不会激活。那么,当应用程序处于后台时,我将如何完成这项工作?有谁知道这个的解决方案吗?
【解决方案2】:

当收到包含alert 键的推送通知时,iOS 会将通知呈现给用户。如果用户与警报交互,您的应用程序将启动到前台模式。

静默通知是包含content-available 键的推送通知。这些通知不呈现给用户,并且不能包含alert、sound 或badge 键。当 iOS 发送静默通知时,您的应用程序将启动到后台模式。应用程序可以在后台执行非常有限的工作以响应静默通知。

无提示通知如下所示:

{
    "aps" : {
        "content-available" : 1
    },
    "com.yourcompany.something.something" : "something"
}

当它被交付时,应用程序委托方法application:didReceiveRemoteNotification:fetchCompletionHandler: 被调用。您对该方法的实现有 30 秒的时间来调用 fetchCompletionHandler 参数中传递的块。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {  
    completionHandler(UIBackgroundFetchResultNoData);
    return;
}

【讨论】:

    【解决方案3】:

    这就是我的解决方案:

    {  
        "aps" : {  
            "content-available" : 1  
        },  
        "acme1" : "bar",  
        "acme2" : 42  
    }  
    

    重要的是把 content-available 设置为 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多