簡體   English   中英

停止重復本地通知iOS

[英]Stop Repeating Local Notification iOS

我每30秒初始化一次本地通知。 而且我想一旦用戶按下停止本地通知的按鈕就停止重復。

問題是我找不到做到這一點的方法。 它持續每30秒重復一次

這就是我教育本地通知的方式

  // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
    localNotification.alertBody = @"Testing Repeating Local Notification";
    localNotification.applicationIconBadgeNumber = 1;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.repeatCalendar = [NSCalendar currentCalendar];

    localNotification.repeatInterval = kCFCalendarUnitSecond;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我已經嘗試過[[UIApplication sharedApplication] cancelAllLocalNotifications]; 但它確實起作用。

您可以將repeatInterval設置為0嗎?根據文檔,如果將其設置為零,則將觸發一次通知。 因此,當按下停止按鈕時,您可以執行以下操作

localNotification.repeatInterval = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];

我解決了

剛剛刪除了localNotification.repeatCalendar = [NSCalendar currentCalendar]; 從上面的代碼。

您可以在應用程序處於活動狀態時收到通知時刪除通知。

就像

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"Application is active");

        /*
        ...
        do some process
        ...
        */

        //remove notification
        [application cancelLocalNotification:notification];
    }
    else
    {
        NSLog(@"Application is inactive");
    }
}

暫無
暫無

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

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