2016-12-14 93 views
0

我使用UILocalNotification,這裏是我的代碼UILocalNotification觸發多次

-(void)notificationScheduler 
{ 
    UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
    // localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7]; 
    localNotification.alertBody = @"NPS message"; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.soundName = UILocalNotificationDefaultSoundName; 
    localNotification.repeatInterval = 0; 
    //Setting badge 
    [UIApplication sharedApplication].applicationIconBadgeNumber += 1; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 

我打電話單擊按鈕操作此方法。通知正在不斷向手機發送。

我嘗試此代碼終止通知

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification{ 
    NSLog(@"didReceiveLocalNotification"); 
    [application cancelLocalNotification:notification]; 

} 

didReceiveLocalNotification方法不獲取調用。

按鈕操作代碼

- (IBAction)btnSendClicked:(id)sender 
{ 

    [self notificationScheduler ]; 

    // saves in dB of sttarter and then reloads tableview to display the message. 

    if (![self.txtMessage.text isEqualToString:@""]) 
    { 
     // publish message 
     [objSttarter SttarterClassPublishTopic:strTopicIdToCompare strData:self.txtMessage.text]; 

     NSMutableDictionary *dctMessage = [[NSMutableDictionary alloc]init]; 
     [dctMessage setValue:@"message" forKey:@"type"]; 
     [dctMessage setValue:@"" forKey:@"timestamp"]; 
     [dctMessage setValue:@"You" forKey:@"from"]; 
     [dctMessage setValue:@"none" forKey:@"file_type"]; 
     [dctMessage setValue:@"" forKey:@"file_url"]; 
     [dctMessage setValue:@"False" forKey:@"sender"];// false = You sent a msg back. 

     NSMutableDictionary *dctPayload =[[NSMutableDictionary alloc]init]; 
     [dctPayload setValue:@"" forKey:@"title"]; 
     [dctPayload setValue:strTopicIdToCompare forKey:@"topic"];//* 
     [dctPayload setValue:self.txtMessage.text forKey:@"message"];//* 
     [dctMessage setValue:dctPayload forKey:@"payload"]; 

     [[DatabaseHandler sharedDatabaseHandler] insertDataToMessages:dctMessage]; 

//  NSIndexPath *path1 = [NSIndexPath indexPathForRow:arrmsg.count inSection:0]; 
//  NSArray *indexArray = [NSArray arrayWithObjects:path1,nil]; 
//  [arrmsg addObject:self.txtMessage.text]; 
//  [self.tableviewChat beginUpdates]; 
//  [self.tableviewChat insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone]; 
//  [self.tableviewChat endUpdates]; 
//  [self.view endEditing:YES]; 
//  [email protected]""; 

    self.txtMessage.text [email protected]""; 

    } 
} 
+0

我認爲'IBAction'是由'UIControlEventTouchUpInside'事件觸發的嗎? – siburb

+0

是的,UIControlEventTouchUpInside只有 – vijeesh

回答

0

我改變的appdelegate didReceiveLocalNotification代碼,這和它的工作。

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    NSLog(@"didReceiveLocalNotification"); 
    [application cancelLocalNotification:notification]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 


} 
+0

這是一個解決方法,但不是解決方案。您不需要取消其他通知。你能告訴我們你觸發通知的代碼嗎?例如按鈕按下代碼。 – siburb

+0

@siburb我在問題中添加了按鈕操作代碼。 – vijeesh