美文网首页
iOS本地通知实现思路

iOS本地通知实现思路

作者: 李昭宏 | 来源:发表于2017-07-10 08:29 被阅读136次

    最近忙毕业的事情,到了6月底才忙完,刚毕业,然后又是公司推新版本,很累很骚,所以到现在才有时间更新博客,其实也是因为大部分时间拿去玩王者荣耀了,辣鸡游戏,大家千万别玩😢
    今天讲讲在做公司新版本的时候,需要实现本地倒计时通知用户的需求,因为之前没有做过本地推送,正好把这个分享一下。

    本地推送(UNUserNotificationCenter,UILocalNotification)

    首先要了解一下在iOS10以上,我们使用UNUserNotificationCenter来实现本地推送
    在iOS8以上,我们使用UILocalNotification来实现本地推送

    UNUserNotificationCenter

    __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
    @interface UNMutableNotificationContent : UNNotificationContent
    
    // Optional array of attachments.
    @property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments __TVOS_PROHIBITED;
    
    // The application badge number. nil means no change. 0 to hide.
    // 通知对应的app图标的红点数量,nil意味着没有改变,0就隐藏红点
    @property (NS_NONATOMIC_IOSONLY, copy, nullable) NSNumber *badge;
    
    // The body of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    //通知的内容体,通常使用[NSString localizedUserNotificationStringForKey:arguments:NSString]传入一个NSString字符串,从而展示通知的内容
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *body __TVOS_PROHIBITED;
    
    // The identifier for a registered UNNotificationCategory that will be used to determine the appropriate actions to display for the notification.
    //和UITableViewCell类似的标识符,可以用通过UNNotificationCategory来判断,从而对通知执行不同操作
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *categoryIdentifier __TVOS_PROHIBITED;
    
    // The launch image that will be used when the app is opened from the notification.
    //通知开启的启动照片
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *launchImageName __TVOS_PROHIBITED;
    
    // The sound that will be played for the notification.
    //通知的提示声音
    @property (NS_NONATOMIC_IOSONLY, copy, nullable) UNNotificationSound *sound __TVOS_PROHIBITED;
    
    // The subtitle of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    //通知的副标题,也是用[NSString localizedUserNotificationStringForKey:arguments:]来初始化
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *subtitle __TVOS_PROHIBITED;
    
    // The unique identifier for the thread or conversation related to this notification request. It will be used to visually group notifications together.
    //对一个通知请求的标识符,通常可以用于管理多个通知
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *threadIdentifier __TVOS_PROHIBITED;
    
    // The title of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
    //通知的标题,用[NSString localizedUserNotificationStringForKey:arguments:]来初始化
    @property (NS_NONATOMIC_IOSONLY, copy) NSString *title __TVOS_PROHIBITED;
    
    // Apps can set the userInfo for locally scheduled notification requests. The contents of the push payload will be set as the userInfo for remote notifications.
    //通知传递的数据,和NSNotification一样
    @property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *userInfo;
    
    @end
    

    UILocalNotification

    @interface UILocalNotification : NSObject<NSCopying, NSCoding>
    
    - (instancetype)init NS_DESIGNATED_INITIALIZER;
    - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
    
    // timer-based scheduling
    // 通知开启的时间,使用NSDate来设置
    @property(nullable, nonatomic,copy) NSDate *fireDate;
    // the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer).
    // pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock).
    //对应所在的时区,通常用[NSTimeZone localTimeZone]
    @property(nullable, nonatomic,copy) NSTimeZone *timeZone;
    //重复时间的方式
    @property(nonatomic) NSCalendarUnit repeatInterval;      // 0 means don't repeat
    //重复时间的时间
    @property(nullable, nonatomic,copy) NSCalendar *repeatCalendar;
    
    // location-based scheduling
    
    // set a CLRegion object to trigger the notification when the user enters or leaves a geographic region, depending upon the properties set on the CLRegion object itself. registering multiple UILocalNotifications with different regions containing the same identifier will result in undefined behavior. the number of region-triggered UILocalNotifications that may be registered at any one time is internally limited. in order to use region-triggered notifications, applications must have "when-in-use" authorization through CoreLocation. see the CoreLocation documentation for more information.
    // 设置一个位置区域,当用户进入或者离开一个区域的时候,会触发推送,使用的时候记得设置关于定位的权限
    @property(nullable, nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
    
    // when YES, the notification will only fire one time. when NO, the notification will fire every time the region is entered or exited (depending upon the CLRegion object's configuration). default is YES.
    // 设置通过位置触发的通知次数是不是只有一次,默认是YES
    @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
    
    // alerts
    // 通知的内容体
    @property(nullable, nonatomic,copy) NSString *alertBody;      // defaults to nil. pass a string or localized string key to show an alert
    // 是否有按钮,默认是有
    @property(nonatomic) BOOL hasAction;                // defaults to YES. pass NO to hide launching button/slider
    // 提示按钮的文字
    @property(nullable, nonatomic,copy) NSString *alertAction;    // used in UIAlert button or 'slide to unlock...' slider in place of unlock
    // 提示按钮的启动页路径
    @property(nullable, nonatomic,copy) NSString *alertLaunchImage;   // used as the launch image (UILaunchImageFile) when launch button is tapped
    // 通知的标题
    @property(nullable, nonatomic,copy) NSString *alertTitle NS_AVAILABLE_IOS(8_2);  // defaults to nil. pass a string or localized string key
    
    // sound
    // 通知的音频名字
    @property(nullable, nonatomic,copy) NSString *soundName;      // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName
    
    // badge
    // 通知的红点
    @property(nonatomic) NSInteger applicationIconBadgeNumber;  // 0 means no change. defaults to 0
    
    // user info
    // 通知的数据
    @property(nullable, nonatomic,copy) NSDictionary *userInfo;   // throws if contains non-property list types
    
    // category identifer of the local notification, as set on a UIUserNotificationCategory and passed to +[UIUserNotificationSettings settingsForTypes:categories:]
    @property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);
    
    @end
    

    代码实践(业务场景是在开启时间5分钟前本地推送提示用户)

    //首先引入头文件
    #ifdef NSFoundationVersionNumber_iOS_9_x_Max
    #import <UserNotifications/UserNotifications.h>
    #endif
    
    if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0f) {
            // 使用 UNUserNotificationCenter 来管理通知
            UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
            
            //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
            UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
            content.title = [NSString localizedUserNotificationStringForKey:@"APP_NAME" arguments:nil];
            content.body = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"【APP_NAME %@】 5分钟后开始抢标",self.model.title]
                                                                 arguments:nil];
            content.sound = [UNNotificationSound defaultSound];
            content.userInfo = @{
                                //对应的数据参数
                                 };
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *date = [dateFormatter dateFromString:self.model.startTime];
            NSTimeInterval startTimeInterval = [date timeIntervalSinceDate:[NSDate date]];
            
            // 在发售时间的5分钟前 推送本地推送
            UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                          triggerWithTimeInterval:startTimeInterval - 5 * 60 repeats:NO];
            
            UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveMethod"
                                                                                  content:content
                                                                                  trigger:trigger];
            
            //添加推送成功后的处理!
            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                //推送失败的话 提示用户
                if(error) {
                    //提示用户
                    
                    return ;
                }
                
                //推送成功的话 提示使用者并且更新UI
            }];
        } else if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f){
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            // 设置触发通知的时间
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            dateFormatter.timeZone = [NSTimeZone localTimeZone];
            NSDate *date = [dateFormatter dateFromString:self.model.startTime];
            
            NSDate *fireDate = [NSDate dateWithTimeInterval:-5 * 60 sinceDate:date];
            
            notification.fireDate = fireDate;
            // 时区
            notification.timeZone = [NSTimeZone defaultTimeZone];
            // 设置重复的间隔
            notification.repeatInterval = kCFCalendarUnitSecond;
            
            // 通知内容
            notification.alertBody = [NSString stringWithFormat:@"【APP_NAME %@】 5分钟后开始抢标",self.model.title];
            notification.alertTitle = @"APP_NAME";
            notification.applicationIconBadgeNumber = 1;
            // 通知被触发时播放的声音
            notification.soundName = UILocalNotificationDefaultSoundName;
            // 通知参数
            notification.userInfo = @{
                                    //对应的数据参数
                                      };
            
            // ios8后,需要添加这个注册,才能得到授权
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
                UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                         categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
                // 通知重复提示的单位,可以是天、周、月
                notification.repeatInterval = 0;
            }
            
            // 执行通知注册
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            
            //推送成功的话 提示用户
            });
        }
    
    //回调处理
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        //iOS8版本下的处理
    }
    
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
            //远程通知
            
        }
        else {
            // 判断为本地通知
            //iOS10下面处理
        }
        
        completionHandler();  // 系统要求执行这个方法
    }
    

    总结

    本地通知其实和远程通知差不多,唯一要小心的就是代码中不要加上移除本地通知的代码,不然会导致本地通知的通知不正常
    [[UIApplication sharedApplication] cancelAllLocalNotifications]
    或者
    [[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification]
    因为公司之前是使用极光推送的,所以上面代码中的iOS10回调是JPush封装好的回调,如果不是使用极光推送的开发者,则是在UNUserNotificationCenterDelegate的中处理回调即可

    @protocol UNUserNotificationCenterDelegate <NSObject>
    
    @optional
    
    // The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
    
    // The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS本地通知实现思路

          本文链接:https://www.haomeiwen.com/subject/ykcyhxtx.html