Hi guys
I’ve a problem with the UNNotification on watchOS 3 !
In watchOS 2 and iOS 9 to schedule a local notification I use WatchConnectivity
From watch I send a message to iPhone and the iPhone schedule the local notification
When the notification arrive with the iPhone locked the watch duplicate the notification and NotificationController in WatchKit Extension call the method
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification
withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler;
that show the notification on watch.
So in watchOS 3 i would like send and receive the notification directly from watch
some code
In watchKit Extension Target i add UserNotifications.framework
In ExtensionDelegate import the framework, add the UNUserNotificationCenterDelegate and request the authorization
#import "ExtensionDelegate.h"
#import <UserNotifications/UserNotifications.h>
@interface ExtensionDelegate()<UNUserNotificationCenterDelegate>{
}
@end
@implementation ExtensionDelegate
- (void)applicationDidFinishLaunching {
/
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
notifCenter.delegate = self;
[notifCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
/
}
}];
}
Then in InterfaceController i schedule an UNNotification
- (IBAction)SendLocalNotification {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@“Test” arguments:nil];
content.subtitle = [NSString localizedUserNotificationStringForKey:@“Local notification from Watch " arguments:nil];
content.sound = [UNNotificationSound defaultSound];
[content setValue:@YES forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
UNTimeIntervalNotificationTrigger* timeTrigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:10 repeats:NO];
NSString *uuid = [[NSUUID UUID] UUIDString];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:uuid
content:content trigger:timeTrigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@" local notification scheduled”);
}
}];
}
(I've also update SendLocalNotification method after reading this post, but I 've not solved the problem)
in NotificationController import the framework and this is the code
#import "NotificationController.h"
#import <UserNotifications/UserNotifications.h>
@interface NotificationController()
@end
@implementation NotificationController
@synthesize label;
- (instancetype)init {
self = [super init];
if (self){
/
/
}
return self;
}
- (void)willActivate {
/
[super willActivate];
}
- (void)didDeactivate {
/
[super didDeactivate];
}
- (void)didReceiveNotification:(UNNotification *)notification
withCompletion:(void(^)(WKUserNotificationInterfaceType interface)) completionHandler {
/
/
/
/
/
[label setText:notification.request.content.subtitle];
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
@end
In ExtensionDelegate i also implement the method to receive the notification when the app is in foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSLog(@“notification arrived in foreground");
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
It’s seem everything ok but ….
- when the watch app is in background don’t receive the notification
- when the watch app is in foreground the console print the log “notification arrived in foreground" but the notification isn’t shown
Where is the problem ?
What i’ve missed ?
I hope someone can help me !!
Thank you so much in advance !!
Vanni