2016-12-14 5 views
0

は私のコード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

関連する問題