显示基于未按预期工作的通知iOS的UIAlertAction
我已经这样做了好几个小时了,据说我的一切都按它应该的样子进行了,但它并没有调用我的显示基于未按预期工作的通知iOS的UIAlertAction,ios,objective-c,ios10,Ios,Objective C,Ios10,我已经这样做了好几个小时了,据说我的一切都按它应该的样子进行了,但它并没有调用我的userNotificationCenter:(UNUserNotificationCenter*)中心 遗嘱提示通知:(未通知*)通知 withCompletionHandler:(void(^)(UNNotificationPresentationOptions))方法当通知在应用程序中发出时,什么也不会发生 当不在应用程序中时,应用程序会按预期发出通知,但在应用程序中不会发出任何通知 这是我的代码,如果你能看
userNotificationCenter:(UNUserNotificationCenter*)中心
遗嘱提示通知:(未通知*)通知
withCompletionHandler:(void(^)(UNNotificationPresentationOptions))
方法当通知在应用程序中发出时,什么也不会发生
当不在应用程序中时,应用程序会按预期发出通知,但在应用程序中不会发出任何通知
这是我的代码,如果你能看到我做错了什么,请告诉我
类NotificationDelegate.h:
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface NotificationDelegate : UNUserNotificationCenter <UNUserNotificationCenterDelegate>
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler;
@end
#import <UIKit/UIKit.h>
#import "NotificationDelegate.h"
@import UserNotifications;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UNUserNotificationCenter *center;
@property (strong, nonatomic) NotificationDelegate *ncdel;
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.h:
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface NotificationDelegate : UNUserNotificationCenter <UNUserNotificationCenterDelegate>
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler;
@end
#import <UIKit/UIKit.h>
#import "NotificationDelegate.h"
@import UserNotifications;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UNUserNotificationCenter *center;
@property (strong, nonatomic) NotificationDelegate *ncdel;
@property (strong, nonatomic) UIWindow *window;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pvHelper = [[PickerViewHelperViewController alloc] init];
self.pickerView.dataSource = self.pvHelper;
self.pickerView.delegate = self.pvHelper;
self.pvHelper.answer = [NSNumber numberWithInteger:10];
}
- (void) requestPermission{
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];
}
- (void) createNotification:(NSNumber *)time{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Alarming, isn't it?" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Listen to me my love" arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.badge = [NSNumber numberWithInteger:99];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:[time integerValue]
repeats:NO];
//Note: Calling addNotificationRequest with the same identifier string will replace the existing notification. If you want to schedule multiple requests use a different identifier each time.
NSString *identifier = @"Alarm Triggered";
UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:req withCompletionHandler:^(NSError * _Nullable error) {
if(error != nil){
NSLog(@"Something went wrong: %@", error);
}
else{
NSLog(@"I have requested the Notification be made");
}
}];
}
- (IBAction)buttonTappedSetAlarm:(id)sender {
[self requestPermission];
[self createNotification: [[self pvHelper] answer]];
}
这并不重要,但我的单视图应用程序只有一个10、20、30…60秒、分钟、小时的选择器,我会计算出请求的时间
正如我所说的,请求在应用程序之外正常工作,但是应用程序在前台,什么也没有发生,这真的让我很困扰
任何帮助都将不胜感激 我知道怎么做了 我不再试图在另一个类中实现
UNUserNotificationCenterDelegate
,而是在我的视图控制器中实现了它:
#import <UIKit/UIKit.h>
#import "PickerViewHelperViewController.h"
@import UserNotifications;
@interface ViewController : UIViewController <UNUserNotificationCenterDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) PickerViewHelperViewController *pvHelper;
// Two methods needed to implement from the UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler;
@end
我上面的创建通知和请求权限方法在代码中保持不变
这允许在应用程序中显示本地通知,如果通过通知打开应用程序,则会显示通知 我知道怎么做了 我不再试图在另一个类中实现
UNUserNotificationCenterDelegate
,而是在我的视图控制器中实现了它:
#import <UIKit/UIKit.h>
#import "PickerViewHelperViewController.h"
@import UserNotifications;
@interface ViewController : UIViewController <UNUserNotificationCenterDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) PickerViewHelperViewController *pvHelper;
// Two methods needed to implement from the UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler;
@end
我上面的创建通知和请求权限方法在代码中保持不变
这允许在应用程序中显示本地通知,如果通过通知打开应用程序,则会显示通知