如何在iOS 10中安排本地通知(objective-c) [英] How to schedule a local notification in iOS 10 (objective-c)

查看:291
本文介绍了如何在iOS 10中安排本地通知(objective-c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用iOS 10安排本地通知.我想知道如何做.我在网上浏览了很多,但是我一直在寻找线索,仅用于注册和处理通知.不是用于安排本地通知.

I'd like to schedule local notifications using iOS 10. I'd like to know how to do this. I've looked all around the web, but I keep finding clues only for registering and handeling the notifications. Not for the scheduling of a local notification.

那么,有人知道该怎么做吗?

So, does anyone know how to do this?

推荐答案

  1. 尝试一下.已弃用但有效的代码.在iOS 10.0之前的版本中使用它:

  1. Try it. Its deprecated but working code. Use it for Before 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];

  1. 对于iOS 10.0和更高版本:现在尝试使用UserNotifications框架:添加该框架,然后像#import一样进行导入.在Appdelegate Didfinishluanch方法中.

  1. For iOS 10.0 and later: Now try with UserNotifications framework: Add the framework, and import like #import . In Appdelegate Didfinishluanch method.

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

在您的练习或方法中,编写并测试:

In your ibaction or method, write it and 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");
    }
}];

这篇关于如何在iOS 10中安排本地通知(objective-c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆