2012-02-12 48 views
6

我已設置每分鐘重複的本地通知,但是我需要應用程序徽章號碼才能每次遞增。當我運行它似乎並沒有增加,它只是保持1。請有人可以幫我嗎?如何遞增重複性本地通知(iPhone)的應用程序徽章號碼

這裏是我創建的通知:

// Create the UILocalNotification 
UILocalNotification *myNotification = [[UILocalNotification alloc] init]; 
myNotification.alertBody = @"Blah blah blah..."; 
myNotification.alertAction = @"Blah"; 
myNotification.soundName = UILocalNotificationDefaultSoundName; 
myNotification.applicationIconBadgeNumber++; 
myNotification.timeZone = [NSTimeZone defaultTimeZone]; 
myNotification.repeatInterval = NSMinuteCalendarUnit; 
myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30]; 
[[UIApplication sharedApplication] scheduleLocalNotification:myNotification]; 
+0

你可以張貼一些代碼的另一端? – HeikoG 2012-02-12 17:49:35

+0

@HeikoG我添加了用於創建通知的代碼。 – 2012-02-12 18:01:13

+0

@TheCrazyChimp你有沒有找到解決這個問題的解決方案? – 2012-05-11 15:42:29

回答

18

做的研究很多的後,我想通了,解決的辦法是,有沒有解決辦法:

iPhone: Incrementing the application badge through a local notification

這是不可能的更新動態地,而你的應用程序的本地通知的徽章數量在後臺。你必須使用推送通知。

+5

雖然這個答案在技術上是正確的,但實際上你可以使用共享的UIApplication實例完成Crazy Chimp想要做的一些額外工作。 您安排一個新的本地通知或應用程序加載每一次,你可以使用「scheduledLocalNotifications」屬性和「cancelAllLocalNotifications」屬性取消所有未來的通知,並通過它們按照時間順序列舉重新分配徽章數到未來本地通知並增加您分配的徽章號碼。 防守並不容易,因爲打字applicationBadgeNumber ++,但它的作品。 – BFar 2014-03-14 06:14:40

+0

+1到該用戶的評論。我測試了這種方法,它工作。這裏去執行一個更完整的解釋:http://stackoverflow.com/a/15461328/2546416 – 2015-02-06 16:35:02

+1

是的,但如果你使用按repeatInterval如果你的應用程序決不會再次啓動,將不會工作。你無法預知徽章值(因爲你有一個每天重複間隔),那麼你就不能增加... – 2015-11-09 13:14:26

-1

嘗試類似:

int plusOne = [myNotification.applicationIconBadgeNumber intValue]; 
plusOne++; 

myNotification.applicationIconBadgeNumber = plusOne; 
+0

不幸的是,這並沒有爲我工作 - 謝謝你的建議。 – 2012-02-12 18:19:43

-1

這應該工作。

myNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
+1

不,男人。它很少工作 – fnc12 2015-02-13 06:45:33

0

雖然沒有簡單applicationIconBadgeNumber++方法,如BFAR提到的,你可以實現你的時候通知添加或刪除更新所有預定UILocalNotifications' applicationIconBadgeNumbers的問。

雖然如果你有使用repeatInterval只要您撥打正確的時間scheduleNotificationdecrementBadgeNumber,類下面應該做的伎倆聲明這是不行的。

@implementation NotificationScheduler 

+ (void) scheduleNotification:(NSString*)message date:(NSDate*)date { 
    UIApplication *app = [UIApplication sharedApplication]; 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    if (notification) { 
     notification.fireDate = date; 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 

     notification.alertBody = message; 
     notification.soundName = UILocalNotificationDefaultSoundName; 
     notification.applicationIconBadgeNumber = [self getExpectedApplicationIconBadgeNumber:date]; 

     [app scheduleLocalNotification:notification]; 
     [self updateBadgeCountsForQueuedNotifiations]; 
    } 
} 

+ (void) decrementBadgeNumber:(long)amount { 
    [self setCurrentBadgeNumber:([self getCurrentBadgeNumber] - amount)]; 
    [self updateBadgeCountsForQueuedNotifiations]; 
} 

+ (long) getExpectedApplicationIconBadgeNumber:(NSDate*)notificationDate { 
    long number = [self getCurrentBadgeNumber]; 
    for (UILocalNotification *notice in [self getScheduledLocalNotifications]) { 
     if (notice.fireDate <= notificationDate) { 
      number++; 
     } 
    } 
    return number; 
} 

+ (void) updateBadgeCountsForScheduledNotifiations { 
    long expectedBadgeNumber = [self getCurrentBadgeNumber]; 
    NSArray *allLocalNotifications = [self getScheduledLocalNotifications]; 
    for (UILocalNotification *notice in allLocalNotifications) { 
     expectedBadgeNumber++; 
     notice.applicationIconBadgeNumber = expectedBadgeNumber; 
    } 
    [[UIApplication sharedApplication] setScheduledLocalNotifications:allLocalNotifications]; 
} 

+ (long) getCurrentBadgeNumber { 
    return [UIApplication sharedApplication].applicationIconBadgeNumber; 
} 

+ (void) setCurrentBadgeNumber:(long)number { 
    [UIApplication sharedApplication].applicationIconBadgeNumber = number; 
} 

+ (NSArray*) getScheduledLocalNotifications { 
    NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES]; 
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]]; 
} 

@end 
+0

它缺少updateBadgeCountsForQueuedNotifiations方法實現......請更新... – 2016-06-15 12:27:44

1

如果您使用外部服務(如Parse for Push),則應該很容易完成。只需在啓動本地通知時增加「解析」徽章編號。雖然這是一個特例。

-1

嘗試......它在簡單的場景,工作對我來說...

notification.applicationIconBadgeNumber = [UIApplication sharedApplication].scheduledLocalNotifications.count + 1; 

而且不要忘記,當應用程序啓動設置徽章圖標回到0。

0

我能做到這一點使用下面的行同時安排本地通知

localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 

並在AppDelegate中

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

    application.applicationIconBadgeNumber -= 1; 
} 
相關問題