如何在ios 10中处理后台推送通知? [英] How to handle push notification in background in ios 10?

查看:423
本文介绍了如何在ios 10中处理后台推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am not handle push notification in background.

对于以下步骤的后台处理推送通知: -

For Handle push notification in background following below steps :-


  1. 在功能 - >启用远程通知。

  2. 在功能 - >后台模式 - >启用远程通知。

  3. 在didFinishLaunchingWithOptions中给予ios 10的所有权限。

  4. 对于使用的推送通知 UNUserNotificationCenter

  5. 应用程序在Foreground中,推送通知工作正常,方法调用如下:

  1. In Capabilities -> Enable Remote notification.
  2. In Capabilities -> Background Mode -> Enable Remote notifications.
  3. In didFinishLaunchingWithOptions give all permission for ios 10.
  4. For push notification used UNUserNotificationCenter.
  5. App In Foreground then push notification is working fine and below method call :

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


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

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.

谢谢。

推荐答案

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 你会得到你需要的东西。

Try to check in didReceiveNotificationResponse you will get what you need.

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

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

根据申请状态处理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 */
    }

这篇关于如何在ios 10中处理后台推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆