我对watchOS 3上的UNNotification有问题!
在watchOS 2和iOS 9中安排本地通知我使用WatchConnectivity 从手表我发送消息到iPhone和iPhone安排本地通知 当通知到达并且iPhone被锁定时,手表会复制通知,而WatchKit Extension中的NotificationController会调用方法
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler;
显示观看通知。
所以在watchOS 3中,我希望直接从手表发送和接收通知 一些代码
在watchKit Extension Target中添加UserNotifications.framework
在ExtensionDelegate中导入框架,添加UNUserNotificationCenterDelegate并请求授权
#import "ExtensionDelegate.h"
#import <UserNotifications/UserNotifications.h>
@interface ExtensionDelegate()<UNUserNotificationCenterDelegate>{
}
@end
@implementation ExtensionDelegate
- (void)applicationDidFinishLaunching {
// Perform any final initialization of your application.
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
notifCenter.delegate = self;
[notifCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
// required to get the app to do anything at all about push notifications
}
}];
}
然后在InterfaceController中我安排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”);
}
}];
}
(我在阅读this post后也会更新SendLocalNotification方法,但我还没有解决问题)
NotificationController中的导入框架,这是代码
#import "NotificationController.h"
#import <UserNotifications/UserNotifications.h>
@interface NotificationController()
@end
@implementation NotificationController
@synthesize label;
- (instancetype)init {
self = [super init];
if (self){
// Initialize variables here.
// Configure interface objects here.
}
return self;
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
- (void)didReceiveNotification:(UNNotification *)notification
withCompletion:(void(^)(WKUserNotificationInterfaceType interface)) completionHandler {
// This method is called when a notification needs to be presented.
// Implement it if you use a dynamic notification interface.
// Populate your dynamic notification interface as quickly as possible.
//
// After populating your dynamic notification interface call the completion block.
[label setText:notification.request.content.subtitle];
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
@end
在ExtensionDelegate中,我还实现了在应用程序处于前台时接收通知的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSLog(@“notification arrived in foreground");
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
看起来一切都还不错......但
- 当手表应用程序在后台时,不会收到通知
- 当手表应用程序位于前台时,控制台会打印日志
notification arrived in foreground
但未显示通知
问题出在哪里? 我错过了什么?
我希望有人可以帮助我!!
提前非常感谢!!
万尼