2016-04-15 15 views
0

通知センターから既に表示されているローカル通知をプログラムで消去する必要があります。後でthis link私は提案された解決策を実装しましたが、この例のeventArrayには常に0個の要素があるという問題があります。私が下にスワイプして通知センターを表示すると、以前に作成した4つの通知が表示されます。だからこの場合、この配列には4つの要素があると思っていますが、0があります。私はiOS 8.3と9.2.1で試したところ、配列は両方とも0です。既に表示されているローカル通知をプログラムで消去する

  1. 通知センターから:左から右へ
    • あなたは、通知をスワイプすることはできません

      のiOSは、ローカル通知を提示する2つの方法があります。

    • 通知を右から左にスワイプすることができます(リストから1つの通知を削除する)。あなたが有効場合にのみ
      • 利用可能:
      • あなたは、あなたのアプリが起動し、通知はロック画面から(iOSのシステムによって処理)通知センター
    • から削除されるまでの通知、をクリックすることができますiPhone/iPad設定のこの設定:http://www.imore.com/how-disable-notification-center-lock-screen-your-iphone-and-ipad
    • 通知を左から右にスワイプできます(アプリはiOSで処理され、開始されます)。 この場合、通知センターからの通知は削除されません(iOSは通知を削除せず、通知センターの通知がすでに通知された後でコードからの通知を1つ削除することはできません)。
    • 通知を右から左にスワイプすることができます(リストからの単一の通知の削除、iOSの処理、通知センター通知の削除、iOSの処理)。
    • 通知をクリックすることはできません。

EDIT:

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.alertBody = @"1"; 
localNotification.alertTitle = @"1"; 
localNotification.userInfo = uniqueDictIdentifier1; 
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; 

UILocalNotification *localNotification2 = [[UILocalNotification alloc] init]; 
localNotification2.alertBody = @"2"; 
localNotification2.alertTitle = @"2"; 
localNotification2.userInfo = uniqueDictIdentifier2; 
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification2]; 

.... 
//2 more notifications are created like this 

そして、すべての通知をフィルタリングするためのコードがある:ここに は、私はそれをやったかのコードサンプルがある

NSArray *eventArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
for (int i=0; i<[eventArray count]; i++) { 
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
    NSDictionary *userInfoCurrent = oneEvent.userInfo; 
    if ([userInfoCurrent isEqualToDictionary:uniqueDictIdentifier1]) { 
     [[UIApplication sharedApplication] cancelLocalNotification:oneEvent]; 
     break; 
    } 
} 

答えて

0

の通知を保存します一意のID

NSDictionary * infoDict = @{ @"alarmUiqueId" : uID, 

            }; 
    NSLog(@"%@",infoDict); 
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond 
                  fromDate:fireDate]; 
    fireDate = [fireDate dateByAddingTimeInterval:-comp.second]; 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = fireDate; 
    localNotif.timeZone = [NSTimeZone localTimeZone]; 
    localNotif.alertBody = desString; 
    localNotif.userInfo = infoDict; 
    localNotif.repeatInterval = NSCalendarUnitDay; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif] 

を削除するには、paritcular通知を削除するには、このコードを書き込みます。

NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

    for(UILocalNotification *notification in notificationArray) 
    { 
     NSLog(@"%@",[notification.userInfo valueForKey:@"alarmUiqueId"]); 
     if ([[notification.userInfo valueForKey:@"alarmUiqueId"] isEqualToNumber: health.uniqueId]) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification] ; 
     } 
    } 
+0

問題の内容をさらに詳しく説明するために質問を編集しました。太字のテキストは、単一の通知を削除する必要がある状況を説明しています。リンクされたstackoverflowの問題には、単一の通知を削除するコードがあります。 –

+0

特定の通知を削除しますか? –

+0

はい、通知センターに既に表示されている1つの通知を削除します。 Gmailアプリをダウンロードして、アプリと同じ問題があることを確認しました。だから、これは私のアプリのバグよりも、iOSの制限だと思う。 –

0

私の理解では、以下の通りです: あなたはscheduledNotificationsで既に通知さLocalNotificationの情報を取得することはできません。

私の解決方法は、アプリケーションのシングルトンオブジェクトにUILocalNotificationインスタンスを保持し、通知センターから削除するときにインスタンスをcancelLocalNotificationにコールすることです。

これはあなたの助けになるでしょうか?

関連する問題