2016-10-31 1 views

Antwort

0

prüfen Antwort von hier akzeptiert: How to schedule a local notification in iOS 10 (objective-c)

  1. es versuchen. Sein veralteter aber funktionierender Code. Verwenden Sie es für Sie vor iOS 10.0:

    //Get all previous noti.. 
    NSLog(@"scheduled notifications: --%@----", [[UIApplication sharedApplication] scheduledLocalNotifications]); 
    
    NSDate *now = [NSDate date]; 
    now = [now dateByAddingTimeInterval:60*60*24*7]; //7 for 7th day of the week. 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    
    [calendar setTimeZone:[NSTimeZone localTimeZone]]; 
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now]; 
    
    
    NSDate *SetAlarmAt = [calendar dateFromComponents:components]; 
    
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
    
    localNotification.fireDate = SetAlarmAt; 
    
    
    NSLog(@"FIRE DATE --%@----",[SetAlarmAt description]); 
    
    localNotification.alertBody [email protected]"Alert"; 
    
    localNotification.alertAction = [NSString stringWithFormat:@"My test for Weekly alarm"]; 
    
    localNotification.userInfo = @{ 
              @"alarmID":[NSString stringWithFormat:@"123"], 
              @"SOUND_TYPE":[NSString stringWithFormat:@"hello.mp3"] 
              }; 
    
        localNotification.repeatInterval=0; //[NSCalendar currentCalendar]; 
    
    
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
    
    1. Für iOS 10.0 und höher: Jetzt versuchen mit UserNotifications Rahmen: Fügen Sie den Rahmen und importieren wie #import. In Appdelegate Didfinishluanch Methode.

      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
          [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) 
             completionHandler:^(BOOL granted, NSError * _Nullable error) { 
              if (!error) { 
               NSLog(@"request succeeded!"); 
               [self testAlrt]; 
              } 
             }]; 
      

In Ihrem IBAction oder Verfahren, schreiben Sie es und Test:

NSDate *now = [NSDate date]; 

// NSLog(@"NSDate--before:%@",now); 

now = [now dateByAddingTimeInterval:60*60*24*7]; 

NSLog(@"NSDate:%@",now); 

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

[calendar setTimeZone:[NSTimeZone localTimeZone]]; 

NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now]; 

NSDate *todaySehri = [calendar dateFromComponents:components]; //unused 



UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; 
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil]; 
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!" 
                    arguments:nil]; 
objNotificationContent.sound = [UNNotificationSound defaultSound]; 

/// 4. update application icon badge number 
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); 


UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO]; 


UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten" 
                     content:objNotificationContent trigger:trigger]; 
/// 3. schedule localNotification 
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
    if (!error) { 
     NSLog(@"Local Notification succeeded"); 
    } 
    else { 
     NSLog(@"Local Notification failed"); 
    } 
}]; 
+0

Dank Jamshed es krank versuchen, und lassen Sie –

+0

weiß, dass ich das für heutigen Tag getan. Du multiplizierst einfach mit 28 und testest. Stellen Sie zunächst den Alarm ein, indem Sie ihn ausführen. Und ändern Sie später das Datum 28 und führen Sie die App erneut aus. Es sollte funktionieren. Mein Code arbeitet an meiner Live-App. –

+0

Jamshed ich muss diese Benachrichtigung für alle 28 Tage wiederholt auslösen muss ich JA in Wiederholungen setzen in UNCalendarNotificationTrigger * trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents: Komponenten wiederholt: NO]; –

Verwandte Themen