简体   繁体   English

如何在ios 10中处理后台推送通知?

[英]How to handle push notification in background in ios 10?

I am not handle push notification in background. 我不是在后台处理推送通知。

For Handle push notification in background following below steps :- 对于以下步骤中的处理推送通知: -

  1. In Capabilities -> Enable Remote notification. 在功能 - >启用远程通知。
  2. In Capabilities -> Background Mode -> Enable Remote notifications. 在功能 - >后台模式 - >启用远程通知。
  3. In didFinishLaunchingWithOptions give all permission for ios 10. 在didFinishLaunchingWithOptions中给出了ios 10的所有权限。
  4. For push notification used UNUserNotificationCenter . 对于使用UNUserNotificationCenter推送通知。
  5. App In Foreground then push notification is working fine and below method call : App In Foreground然后推送通知工作正常并且在方法调用之下:

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

But my problem is app in background then not call any method.so any one have idea or solution for handle push notification in background for ios 10 then please help me. 但我的问题是app在后台然后没有调用任何方法。所以任何人有想法或解决方案的背景为ios 10处理推送通知然后请帮助我。

Thanks. 谢谢。

willPresentNotification is called when app is in foreground. 当app在前台时调用willPresentNotification Have a look to their documentation 看看他们的文档

 - (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:.

}

Try to check in didReceiveNotificationResponse you will get what you need. 尝试检查didReceiveNotificationResponse你会得到你需要的。

ALSO If need to fetch any data or any processing, Enable background fetch in background modes and use below method 另外如果需要获取任何数据或任何处理,请在后台模式下启用后台获取并使用以下方法

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

    completionHandler(UIBackgroundFetchResultNewData);
}

Handling APNS on the basis of application states 根据应用程序状态处理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 */
    }

This was the solution for me: 这是我的解决方案:

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

What is important is to put the content-available to 1. 重要的是将内容可用于1。

When a push notification containing the alert key is received iOS will present the notification to the user. 当接收到包含alert键的推送通知时,iOS将向用户呈现通知。 If the user interacts with the alert your application will be launched into the foreground mode. 如果用户与警报交互,您的应用程序将启动到前台模式。

Silent notifications are push notifications that contain the content-available key. 静默通知是包含content-available密钥的推送通知。 These notifications are not presented to the user and can't contain the alert , sound , or badge keys. 这些通知不会呈现给用户,也不能包含alertsoundbadge键。 When iOS delivers a silent notification your application is launched into the background mode. 当iOS发出静默通知时,您的应用程序将启动到后台模式。 The application can perform a very limited amount of work in the background in response to the silent notification. 应用程序可以在后台执行非常有限的工作量以响应静默通知。

A silent notification looks like this: 静默通知如下所示:

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

When it is delivered the application delegate method application:didReceiveRemoteNotification:fetchCompletionHandler: is called. 当它被传递时,应用程序委托方法application:didReceiveRemoteNotification:fetchCompletionHandler:被调用。 Your implementation of that method has 30 seconds to call the block passed in the fetchCompletionHandler parameter. 您对该方法的实现有30秒的时间来调用fetchCompletionHandler参数中传递的块。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM