目标C iOS 12本地通知shouldAlwaysAlertWhileAppIsForeground强制关闭

时间:2019-03-20 09:27:03

标签: ios objective-c

我有以下代码,并且在iOS 11之下运行良好。

// 申请通知权限
                        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {

                            // A Boolean value indicating whether authorization was granted. The value of this parameter is YES when authorization for the requested options was granted. The value is NO when authorization for one or more of the options is denied.

                            if (granted) {

                                // 1、创建通知内容,注:这里得用可变类型的UNMutableNotificationContent,否则内容的属性是只读的
                                UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

                                NSString* buysell;
                                if([trade.bidAsk isEqualToString:@"B"]){
                                    buysell = @"Success BUY";
                                }else{
                                    buysell = @"Success SELL";
                                }
                                // 标题
                                content.title = buysell;
                                // 次标题
                                //content.subtitle = trade.productKey.symbol;
                                // 内容
                                SpecialNumberFormat* formatter = [[SpecialNumberFormat alloc] init];
                                NSString* symbol = trade.productKey.symbol;
                                NSString* tradePrice = [formatter format:trade.exePrice];
                                NSString* tradeQty = [formatter format:trade.exeQty];
                                NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
                                [timeFormat setDateFormat:@"HH:mm:ss"];
                                NSString* time = [timeFormat stringFromDate:trade.exeTime];

                                NSString* cbody = [NSString stringWithFormat: @"%@, Time: %@, Price: %@, Qty: %@", symbol, time, tradePrice, tradeQty];
                                NSLog(@"%@", cbody);
                                content.body = cbody;

                                // 通知的提示声音,这里用的默认的声音
                                content.sound = [UNNotificationSound defaultSound];

                                //NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"jianglai" withExtension:@"jpg"];
                                //UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageIndetifier" URL:imageUrl options:nil error:nil];

                                // 附件 可以是音频、图片、视频 这里是一张图片
                                //content.attachments = @[attachment];

                                // 标识符
                                content.categoryIdentifier = @"categoryIndentifier";
                                [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];

                                // 2、创建通知触发
                                /* 触发器分三种:
                                 UNTimeIntervalNotificationTrigger : 在一定时间后触发,如果设置重复的话,timeInterval不能小于60
                                 UNCalendarNotificationTrigger : 在某天某时触发,可重复
                                 UNLocationNotificationTrigger : 进入或离开某个地理区域时触发
                                 */
                                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

                                // 3、创建通知请求
                                UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:@"Notification" content:content trigger:trigger];

                                // 4、将请求加入通知中心
                                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
                                    if (error == nil) {
                                        NSLog(@"已成功加推送%@",notificationRequest.identifier);
                                    }
                                }];
                            }
                        }];

但由于iOS 12已删除了shouldAlwaysAlertWhileAppIsForeground而被强制关闭。 我该怎么做才能解决这个问题?如今,它对Objective-C的引用越来越少,所以我可以用完整的代码作为示例来解决此问题吗?

0 个答案:

没有答案