我有一个项目,我需要 iOS 从后台的 api 检查到期日期并在该日期安排通知,但是,只要代码点击 http.get 调用,它就会在 Xcode 中停止。我必须在 Xcode 中运行才能模拟后台提取。
这是我的 main.dart:
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart' as w;
List<String> accountList = [];
List<String> notificationList = [];
List<dynamic> dateInfo = [];
List<dynamic> accountInfo;
SharedPreferences myPrefs;
bool switchOne = true;
bool switchTwo = true;
bool switchThree = true;
//this is the name given to the background fetch
const simplePeriodicTask = "simplePeriodicTask";
w.Workmanager workmanager = w.Workmanager();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await workmanager.initialize(callbackDispatcher, isInDebugMode: true);
await NotificationService().initialize();
runApp(BWSApp());
}
void setSwitches() async {
WidgetsFlutterBinding.ensureInitialized();
myPrefs = await SharedPreferences.getInstance();
switchOne = (myPrefs.getBool('switchOne') ?? false);
switchTwo = (myPrefs.getBool('switchTwo') ?? false);
switchThree = (myPrefs.getBool('switchThree') ?? false);
if(myPrefs.getStringList('accounts') != null){
for(var i = 0; i < myPrefs.getStringList('accounts').length; i++){
accountList.add(myPrefs.getStringList('accounts')[i]);
notificationList.add(myPrefs.getStringList('notificationId')[i]);
http.Response result = await http.get(Uri.parse("https://reqres.in/api/users?page=2"));
print("doesn't even get here");
_getDueDate(json.decode(result.body), myPrefs.getStringList('notificationId')[i]);
}
}
}
void callbackDispatcher() {
print("callBackDispatcher");
workmanager.executeTask((task, inputData) async{
setSwitches();
return Future.value(true);
});
}
这是我的 AppDelegate.swift:
import UIKit
import Flutter
import workmanager
import shared_preferences
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
WorkmanagerPlugin.register(with: self.registrar(forPlugin: "be.tramckrijte.workmanager.WorkmanagerPlugin")!)
UNUserNotificationCenter.current().delegate = self
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
AppDelegate.registerPlugins(with: registry)
FLTSharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
static func registerPlugins(with registry: FlutterPluginRegistry) {
GeneratedPluginRegistrant.register(with: registry)
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
}
在 Xcode 中,我模拟了后台获取,我可以打印一些东西,直到它到达 http.get 然后它看起来就像它刚刚停止。任何帮助表示赞赏。