Reputation: 541
i have tried this Code in Appdelegates.
@import UserNotifications;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
center.delegate = self;
}
}];
And the local notification is fired by this code
-(void)localNotification{
NSLog(@"local notification fired");
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = @"local notificatin";
objNotificationContent.body = @"message";
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:0.3 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"three"
content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
}
But Local notification is not displayed. As i have used two Delegates method one is for present notification in foreground and one method that Must be called when local notification received.
that Delegates methods does not called at any Scenario.
please find Silly Mistake where i m wrong
Upvotes: 3
Views: 7479
Reputation: 4959
You are unable to see the local notification because your application might be in foreground. Local Notification doesn't show up if you are in foreground.
Your code is okay, but I would suggest to make some changes & test your app again.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
applicationDidEnterBackground
[self localNotification];
Show Notification on Foreground
If you want to show notification when your application is in foreground then, implement
Conform UNUserNotificationCenterDelegate
in AppDelegate.h
Add following code in AppDelegate.m
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
Upvotes: 9
Reputation: 485
If you're in foreground, your delegate method will be called but no visual notification will be displayed. If you really want to show a notification in foreground you can use something like:
https://github.com/pikacode/EBForeNotification
Upvotes: 0