2012-12-10 44 views
2

我現在在應用程序中使用本地通知,但是我發現有些奇怪。關於iOS上本地通知的問題

我設置並安排了這樣的通知。

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
if (localNotif == nil) { 
    return; 
} 
NSDate *now = [[NSDate alloc] init]; 
now = [now dateByAddingTimeInterval:dayToFinish * 24 * 60 * 60]; 
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:now]; 

components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now]; 
int month = [components month]; 
int day = [components day]; 
int year = [components year]; 

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
[dateComps setYear:year]; 
[dateComps setMonth:month]; 
[dateComps setDay:day]; 
[dateComps setHour:18]; 
[dateComps setMinute:15]; 
[dateComps setSecond:0]; 

//There are a lot to set up the fire date, you could ignore it. 
NSDate *fireDate = [calendar dateFromComponents:dateComps]; 

localNotif.fireDate = fireDate; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
localNotif.alertBody = [NSString stringWithFormat:@"Test message %@", self.name]; 
localNotif.applicationIconBadgeNumber = 1; 

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.name forKey:@"ListRecordName"]; 
localNotif.userInfo = infoDict; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

這部分代碼可以調用多次來安排幾個本地通知,現在奇怪的事情來了。

  1. 雖然通知中心中有多個條目,但徽章號仍然是一個。
  2. 一旦我點擊其中一個通知,所有其他通知就會消失。但我沒有使用cancelAllLocalNotifications方法。

我該如何解決這個問題,謝謝。

+0

那麼你有什麼嘗試... –

+0

localNotif.applicationIconBadgeNumber = 1;此代碼顯示徽章一次(您認爲是什麼)... –

+0

http://m.youtube.com/#/watch?v=ysm0QKs2a3Q&desktop_uri=%2Fwatch%3Fv%3Dysm0QKs2a3Q查看此鏈接.. –

回答

8

當您的應用程序位於後臺時,無法使用本地通知動態更新徽章號碼。你必須使用推送通知。

+1

對於屬性applicationIconBadgeNumber它沒有意義,正如文檔中的描述所述,它應該根據此屬性更新徽章號,並且它與當點擊。 – seanxiaoxiao