问题
I am unable to fire a UserNotification
in iOS 10. I will start with my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound);
center.delegate = self;
[center requestAuthorizationWithOptions: options
completionHandler: ^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"Granted notifications!");
}
}];
//
This works, I see the log.
Then, I have:
-(void)application: (UIApplication *)application performFetchWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {
//
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = NSLocalizedString(@"Updates are available", nil);
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 1
repeats: NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: @"IDLocalNotification"
content: content
trigger: trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest: request withCompletionHandler: ^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Notification was requested!");
}
}];
Again, this works, I see the log.
But I don't see a notification appear on my screen (the trigger delay is only 1 sec, but I tried larger values as well).
Also, the willPresentNotification
delegate method is never reached.
What am I missing?
回答1:
Found the problem.
It turns out that I need to set the body
of the content, not just the title
:
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = [NSString localizedUserNotificationStringForKey: @"Updates are available" arguments: nil];
I can even leave the title
out and it works.
来源:https://stackoverflow.com/questions/41785105/usernotification-is-not-showing-ios-10