gpt4 book ai didi

ios - 如何从 iOS 中的后台任务打开应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:54 25 4
gpt4 key购买 nike

是否可以从 iOS 中的后台任务打开应用程序?

我希望我的应用程序在后台运行,并且无论出于何种原因,我都希望它在没有用户输入的情况下重新出现。这可能吗?

在 Android 中,您可以拥有在后台运行的服务,并且您可以随时启动一个 Activity,该 Activity 会启动该 Activity 的应用。

我知道这不会带来良好的用户体验。还是想知道能不能实现。

最佳答案

您可以创建时间偏移量为 0 的本地通知计划(例如立即显示警报)。如果用户点击通知,应用程序将启动。

否则,您将无法在没有通知的情况下从后台启动到前台。

    NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:0]; //

UIApplication* app = [UIApplication sharedApplication];
NSArray * oldNotifications = [app scheduledLocalNotifications];


// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];

// Create a new notification.
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"sonar";
alarm.alertBody = @"Background task complete, tap to show app" ;

[app scheduleLocalNotification:alarm];
}

来自苹果文档:“通知是应用程序暂停、在后台或未运行时引起用户注意的一种方式。应用程序可以使用本地通知来显示警报、播放声音、标记应用程序的图标或三者的组合. 例如,闹钟应用程序可能会使用本地通知来播放闹钟声音并显示警报以禁用闹钟。当向用户发送通知时,用户必须决定该信息是否值得将应用程序带回前台.(如果应用程序已经在前台运行,本地通知会悄悄地传递给应用程序而不是用户。)”

关于ios - 如何从 iOS 中的后台任务打开应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com