2015-04-18 80 views
0

我目前正在嘗試取消與我的數據模型中的對象相關的特定UILocalNotifications。爲此,每個數據對象都有一個唯一的標識符,即NSUUID取消UILocalNotification

創建UILocalNotification:

/* Set UILocalNotification */ 
     UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
     localNotification.fireDate = date; 
     localNotification.alertBody = self.mytextfield.text; 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 
     localNotification.applicationIconBadgeNumber = 1; 
     NSLog(@"making a notification: %@",[r.uniqueId UUIDString]); 
     [localNotification.userInfo setValue:[r.uniqueId UUIDString] forKey:@"uid"]; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

然而,當我去刪除通知,並打印出每個通知內容,通知的alertbody正確打印,但唯一標識符被莫名其妙地丟失。這個實現有什麼問題?

取消UILocalNotification:

Obj *r = (Obj *)[self.objects[indexPath.section] objectAtIndex:indexPath.row]; 

    /* Cancel UILocalNotification */ 
    NSArray *scheduleReminders = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    NSLog(@"scheduled.count: %i",(int)scheduleReminders.count); // Correctly prints 3 
    for (int i = 0; i < scheduleReminders.count; i++) 
    { 
     UILocalNotification *notification = scheduleReminders[i]; 

     NSDictionary *userInfoCurrent = notification.userInfo; 
       NSLog(@"searching through reminders: %i %@ %@ %@",(int)i,[userInfoCurrent objectForKey:@"uid"], notification.alertBody); // Alert body prints correctly 

     if ([[userInfoCurrent valueForKey:@"uid"] isEqualToString:[r.uniqueId UUIDString]]) 
     { 
      NSLog(@"found it"); 
      //Cancelling local notification 
      [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
      break; 
     } 
    } 

回答

3

的問題是,用戶信息是 「無」 默認情況下。您應該分配您自己的NSDictionary,設置uid,然後將此字典設置爲localNotification.userInfo。

+0

啊,我沒有意識到這一點。謝謝你解決了這個問題。 – Apollo