Nativescript,在 ios 委托中注入 angular 服务

Nativescript, inject angular service within ios delegate

我正在使用 angular 和 nativescript,我想在我的项目中使用 angular 服务从 api 获取一些数据,并使用该数据推送本地通知.我想使用 Nativescript 文档中提到的示例在后台推送此通知

https://docs.nativescript.org/core-concepts/ios-runtime/how-to/BackgroundExecution.

该方法如下所示:

import { LocalNotifications } from 'nativescript-local-notifications';

export class BackgroundRefreshAppDelegate extends UIResponder implements UIApplicationDelegate {
    public static ObjCProtocols = [UIApplicationDelegate];
    public static ObjCExposedMethods = {
        "runOnBackground": { returns: interop.types.void }
    };

    private bgTask;
    private timer;
    private timerCounter;

    public applicationPerformFetchWithCompletionHandler(application: UIApplication, completionHandler: any) {
        console.log('App is running in background');

        // Check for new data
        const newData = true;

        // If new data exists, initiate a local notification
        if (newData) {
            LocalNotifications.schedule([{
            title: 'test',
          }]).then(
              function() {
                console.log('Notification scheduled');
              },
              function(error) {
                console.log('scheduling error: ' + error);
              }
          );

            // Let the OS know that there were new data and complete the task
            completionHandler(UIBackgroundFetchResult.NewData)
        } else {
            // Otherwise, let the OS know there is no new data and complete the task
            completionHandler(UIBackgroundFetchResult.NoData);
        }

    }
}

如何在此 ios 委托中注入 angular 服务并在 applicationPerformFetchWithCompletionHandler 方法中使用它?

你不能/不应该这样做,后台服务 运行 在 Angular 上下文之外。

改为监听您服务上的自定义事件并从您的 applicationPerformFetchWithCompletionHandler 触发自定义事件。