后台抓取不起作用-无法触发通知

时间:2018-07-16 16:15:18

标签: ios swift background-fetch

我正在尝试为我的应用(如以下配置)实现Background Fetch API。

我已启用“从功能中进行后台获取”。

在AppDelegate.swift中

通过didFinishLaunchingWithOptions方法添加了此

UIApplication.shared.setMinimumBackgroundFetchInterval(30)

也执行了此方法以执行任务。

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    debugPrint("New notification fired from AppDelegate...!!")

    let notif = UNMutableNotificationContent()
    notif.title = "New notification from App delegate"
    notif.subtitle = "Cool App!"
    notif.body = "I liked it!"

    UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .badge, .alert], completionHandler: { (isGranted, error) in

        DispatchQueue.main.async {
            let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
            let request = UNNotificationRequest(identifier: "myNotification", content:  notif, trigger: notifTrigger)

            UNUserNotificationCenter.current().add(request) { (error) in
                if error != nil{
                    print(error!)
                } else {
                    // do something
                }
            }
        }
    })

}

配置所有内容后,本地通知不触发。为什么这样?

我想念什么吗?

我也尝试过this tutorial

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您没有在 // My Method to get Sub Classes private void GetClassTree() { //Create A Instance from Class GetOrders obj = new GetOrders(); //Add main class to TreeView treeView1.Nodes.Add(obj.GetType().Name, obj.GetType().Name); // Get all classes inside the created instance of main class foreach (MemberInfo Item in obj.GetType().GetMembers()) { // Add sub classes in first level treeView1.Nodes[obj.GetType().Name].Nodes.Add(Item.Name, Item.Name); foreach (MemberInfo SubItem in Item.GetType().GetMembers()) { // Add sub classes in second level treeView1.Nodes[Item.Name].Nodes.Add(Item.Name); // Check if there is sub class in third level if (SubItem.GetType().GetMembers() != null) { foreach (MemberInfo SubIteml1 in SubItem.GetType().GetMembers()){ // Add sub classes in third level treeView1.Nodes[SubItem.Name].Nodes.Add(SubIteml1.Name); } } } } } 中调用completionHandler。我可以使用以下代码测试performFetchWithCompletionHandler

BackgroundFetch

您可以通过以下步骤测试“后台抓取”:

  1. 运行您的应用程序。
  2. 转到Xcode工具栏。
  3. 选择“调试”>“模拟后台提取”。

现在,您将可以测试后台提取。

答案 1 :(得分:0)

对此有类似的问题:

Background Fetch Does Not Appear to Fire

  

如果fetch事件适用于以下情况,请尝试强制其在模拟器上运行   模拟器,证明一切设置正确。有   除了等待,您无能为力。

相关问题