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

8

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

要在后台处理推送通知,请按以下步骤进行:

  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
    

我的问题是当应用程序在后台时,不调用任何方法。如果有人对如何处理iOS 10后台推送通知的想法或解决方案,请帮助我。

谢谢。


嗨,我有一个类似的情况,但需要稍作修改,例如当我收到推送通知时,应用程序应在后台激活并执行给定的任务(如用户位置更新代码)-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo,但我的应用程序直到用户点击通知才会激活。那么,在应用程序处于后台时,我该如何完成此操作?有人知道解决方案吗? - Moxarth
3个回答

7

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,您将获得所需内容。

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

-(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? - shraddha k vaishanani
你好,Abhishek。我尝试了你的解决方案,但它仍然无法正常工作。如果有其他解决方案,请帮助我。 - shraddha k vaishanani
静默通知将调用didReceiveRemoteNotification函数。 - Abhishek Thapliyal
@AbhishekThapliyal,请问您可以看一下这个问题吗?https://stackoverflow.com/questions/46212370/update-sqlite-db-from-push-notification-app-is-closed-and-not-running-in-backgr/ - chetan patel
1
嗨,我有一个类似的情况,但需要稍作修改,例如当我收到推送通知时,应用程序应在后台激活并执行给定的任务(如用户位置更新代码)-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo,但我的应用程序直到用户点击通知才会激活。那么,在应用程序处于后台时,我该如何完成此操作?有人知道解决方案吗? - Moxarth
显示剩余6条评论

3
当接收到包含 "alert" 键的推送通知时,iOS 将向用户呈现该通知。如果用户与警报进行交互,则您的应用程序将启动到前台模式。
无声通知是包含 "content-available" 键的推送通知。这些通知不会呈现给用户,并且不能包含 "alert"、"sound" 或 "badge" 键。当 iOS 传递静默通知时,您的应用程序将启动到后台模式。应用程序可以对静默通知做出非常有限的响应工作。
静默通知如下所示:
{
    "aps" : {
        "content-available" : 1
    },
    "com.yourcompany.something.something" : "something"
}

当应用程序调用代理方法application: didReceiveRemoteNotification: fetchCompletionHandler:时,将被触发。您需要在该方法的实现中调用fetchCompletionHandler参数中传递的块,在30秒内完成。

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

2
这对我来说是解决方案:
{  
    "aps" : {  
        "content-available" : 1  
    },  
    "acme1" : "bar",  
    "acme2" : 42  
}  

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接