簡體   English   中英

在應用程序處於后台狀態時獲取本地通知?

[英]Get the local notification when the app is in background state?

我想開發一個提醒應用程序。 它應該每周,每月,每季度,六個月和每年提醒人們。

我使用NSDate屬性獲取當前日期,然后將7天添加到當前日期,添加7天后的日期將存儲在plist中。

我選擇電話/郵件/消息按鈕來提醒人們。 在按鈕操作中,我將當前日期與保存在plist中的日期進行比較,然后設置本地通知。 但是根據通知日期,本地通知無法正常工作。 當應用程序在前台運行時,它可以正常工作,但是在后台運行時,則無法運行。

您必須安排警報。 這是Apple文檔的摘錄:

- (void)scheduleAlarmForDate:(NSDate*)theDate
{
    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 = @"alarmsound.caf";
        alarm.alertBody = @"Time to wake up!";

        [app scheduleLocalNotification:alarm];
    }
}

您可以在Apple文檔“應用程序狀態和多任務處理”中找到很多有用的信息,特別是在“背景執行和多任務處理”部分中 (它涵蓋了您的用例)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM