2014-09-24 37 views
0
  • evenArray[]包含的本地通知
  • 名單我有創造localnotification功能,但它在編輯調用,並添加模塊,因此多次調用並複製本地通知
  • 請讓我知道情況如何檢查在這個數組中通知我當前的通知,我將只在它不重複時創建。

這是一個參數,我會需要我的通知比較這個「144 = MAS」通知144是唯一的,指數也獨特如何防止ios中的本地通知重複?

"<UIConcreteLocalNotification: 0x118b08c0>{fire date = Monday, September 29, 2014 at 6:12:00 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, September 29, 2014 at 6:12:00 PM India Standard Time, user info = {\n 144 = mas;\n index = 63;\n userid = \"821e25e7-d76c-4328-bbba-8e367dc70c04\";\n}}", 
"<UIConcreteLocalNotification: 0x118b0cc0>{fire date = Monday, September 29, 2014 at 6:12:00 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, September 29, 2014 at 6:12:00 PM India Standard Time, user info = {\n 153 = mbg;\n index = 64;\n userid = \"821e25e7-d76c-4328-bbba-8e367dc70c04\";\n}}" 

回答

2

可以防止它通過使用[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)]清除之前的通知[[UIApplication sharedApplication] cancelAllLocalNotifications]

您還有定製通知的NSArray在[[UIApplication sharedApplication] scheduledLocalNotifications]

使用這些方法,您應該能夠管理您的通知。

1

當您在編輯記錄時閱讀完您的問題之後,我會理解您在編輯時添加的時間裏添加的相同通知。如果它是正確的,則在編輯記錄時需要刪除先前的通知。如果您有多條記錄並且針對該記錄有多條通知,則需要爲該通知提供一些唯一標識,以便您可以輕鬆識別需要刪除的通知。

我也面臨同樣的問題,所以我有一個獨特的價值userInfo字典作爲一個整數,每當我添加或編輯通知,首先我檢查它,如果它已經添加刪除它,並添加另一個,因爲我我的情況我想根據編輯的記錄通知。

我使用的代碼在這裏,可能會對你有幫助。

-(void)scheduleNotification:(NSString *)remainderText :(BOOL)isRepeat :(NSDate *)fireDate :(NSString *)notificationId{ 

    NSDictionary *dict=[[NSMutableDictionary alloc]init]; 
    [dict setValue:@"NotificationID" forKeyPath:notificationId]; 
    UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 

    [email protected]"Your Msg"; 
    localNotification.timeZone=[NSTimeZone localTimeZone]; 


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


    localNotification.fireDate=fireDate; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 

-(void) deleteNotification:(NSString *)curentNotificationId 
{ 
    UIApplication *app = [UIApplication sharedApplication]; 
    NSArray *eventArray = [app scheduledLocalNotifications]; 


    for (int i=0; i<[eventArray count]; i++) 
    { 
     UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
     NSDictionary *userInfoCurrent = oneEvent.userInfo; 
     NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"NotificationID"]]; 

     if ([uid isEqualToString:curentNotificationId]) 
     { 
      //Cancelling local notification 
      [app cancelLocalNotification:oneEvent]; 
     } 
    } 

} 

檢查它..寫在日程安排的通知方法等的事情,你想要的,並記住notificationID使用它傳遞給scheduleNotification方法應該是唯一的

+0

在我的代碼中,當我點擊單個通知時,它從通知中心全部清除。即使我已經分配了uid。 – user3743909 2014-09-24 11:01:24

+0

使用此代碼可能對您有所幫助 – user2493047 2014-09-24 11:40:02

-1

如果你想刪除下面

具體使用的通知

在UIApplication.sharedApplication通知()。scheduledLocalNotifications

{

UIApplication.sharedApplication()。cancelLocalNotification(通知)

}

或者

如果你想刪除下面

[UIApplication的sharedApplication] cancelAllLocalNotifications]通知所有使用。