如何在iOS 10中安排本地通知(objective-c)

如何在iOS 10中安排本地通知(objective-c),第1张

概述我想使用iOS 10安排本地通知.我想知道如何做到这一点.我一直在网上看,但我一直在寻找线索,只是为了注册和处理通知.不适用于本地通知的安排. 那么,有谁知道怎么做? >试试吧.它已弃用但工作代码.在iOS 10.0之前使用它: //Get all previous noti.. NSLog(@"scheduled notifications: --%@----", [[UIApplicatio 我想使用iOS 10安排本地通知.我想知道如何做到这一点.我一直在网上看,但我一直在寻找线索,只是为了注册和处理通知.不适用于本地通知的安排.

那么,有谁知道怎么做?

解决方法 >试试吧.它已弃用但工作代码.在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 =@"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];

>对于iOS 10.0及更高版本:现在尝试使用UserNotifications框架:添加框架,并像#import一样导入.在Appdelegate DIDfinishluanch方法.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    [center requestAuthorizationWithOptions:(UNAuthorizationoptionBadge | UNAuthorizationoptionSound | UNAuthorizationoptionAlert)              completionHandler:^(BOol granted,NSError * _Nullable error) {                  if (!error) {                      NSLog(@"request succeeded!");                      [self testAlrt];                  }              }];

在你的ibaction或方法中,写下它并测试:

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]; //unusedUNMutableNotificationContent *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 numberobjNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];UNNotificationRequest *request = [UNNotificationRequest requestWithIDentifIEr:@"ten"                                                                      content:objNotificationContent trigger:trigger];/// 3. schedule localnotificationUNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {    if (!error) {        NSLog(@"Local Notification succeeded");    }    else {        NSLog(@"Local Notification Failed");    }}];
总结

以上是内存溢出为你收集整理的如何在iOS 10中安排本地通知(objective-c)全部内容,希望文章能够帮你解决如何在iOS 10中安排本地通知(objective-c)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109138.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存