我对 watchOS 3 上的 UNNotification 有疑问!
在 watchOS 2 和 iOS 9 中安排本地通知我使用 WatchConnectivity 从手表我向 iPhone 发送消息,iPhone 安排本地通知当通知到达时 iPhone 锁定手表复制通知和 WatchKit 扩展中的 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”);
}
}];
}
(读完这篇文章后我也更新了 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
但未显示通知
问题出在哪里 ?我错过了什么?
我希望有一个人可以帮助我 !!
非常感谢您!
万尼