我一直在這裏待了好幾個小時,據說我有一切正常工作,但它不會在應用程序中發出通知時調用我的userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))
方法,發生在所有。顯示基於UNUserNotification的UIAlertAction iOS沒有按預期工作
該應用程序提供了一個通知,預計在不應用程序,但沒有同時。
這裏是我的代碼,如果你能看到的東西我做錯了,請告訴我。
類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
NotificationDelegate.m:
#import "NotificationDelegate.h"
@implementation NotificationDelegate
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
[self sendMessage:[UIApplication sharedApplication] message:notification.request.content];
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
- (void) sendMessage:(UIApplication *)application message:(UNNotificationContent *)content{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:content.title
message:content.body
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Go Away!" style:UIAlertActionStyleDefault handler:nil];
[ac addAction:action];
dispatch_async(dispatch_get_main_queue(), ^{
[application.keyWindow.rootViewController presentViewController:ac animated:YES completion:nil];
});
}
//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
}
@end
AppDelegate.h:
#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.m:
#import "AppDelegate.h"
@import UserNotifications;
@interface AppDelegate()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.center = [UNUserNotificationCenter currentNotificationCenter];
self.center.delegate = self.ncdel;
return YES;
}
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秒,分鐘,小時,選擇器,和我做的數學來獲得請求的時間。
所以就像我說的那樣,請求在應用程序之外按預期工作,但是在前臺應用程序中,什麼都沒有發生,並且它真的困擾着我。
任何幫助將不勝感激。