繁体   English   中英

WatchKit上没有收到达尔文通知

[英]WatchKit the Darwin Notification is not received on the watch

我想从我的iPhone使用监视设备发送通知CFNotificationCenterAddObserver对手表和CFNotificationCenterPostNotification 。(我不是在Xcode的模拟器测试)。

这是我在iOS应用中的代码:

#include <CoreFoundation/CoreFoundation.h>
...
- (void)sendLogOutNotificationToWatch{
    dispatch_async(dispatch_get_main_queue(), ^{
        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
    });
}

这就是我在Apple Watch扩展应用上使用它的方式:

@implementation InterfaceController
.....
- (void)awakeWithContext:(id)context {

    [super awakeWithContext:context];
    ....
    [self registerToNotification];
}

- (void)registerToNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@com.test.app" object:nil];
    CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoggedOut ) name:@"com.test.app" object:nil];
    CFNotificationCenterAddObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), didReceivedDarwinNotification, CFSTR( "NOTIFICATION_TO_WATCH" ), NULL, CFNotificationSuspensionBehaviorDrop );

}

void didReceivedDarwinNotification()
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"com.test.app" object:nil];
}


- (void)didDeactivate {
    [super didDeactivate];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"com.test.app" object:nil];
    CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
- (void)userLoggedOut{
    [self showAlertViewwithTitle:@"Notice" andMessage:@"User logged out on iPhone device!"];
}

您应该使用WatchConnectivity将消息从iPhone发送到Apple Watch。

Watch和iPhone上的API几乎相同。 如果您的手表应用未运行,或者屏幕关闭,则应使用transferUserInfo。 如果您的手表应用程序正在运行并且屏幕打开,则可以使用sendMessage。 我通常包装这些调用,尝试首先使用sendMessage,如果失败,请使用transferUserInfo:

// On the iPhone
func trySendMessage(message: [String : AnyObject]) {
    if self.session != nil && self.session.paired && self.session.watchAppInstalled {
        self.session.sendMessage(message, replyHandler: nil) { (error) -> Void in
            // If the message failed to send, queue it up for future transfer
            self.session.transferUserInfo(message)
        }
    }
}

在手表上,您将需要同时实现session:DidReceiveMessage和session:didReceiveUserInfo。 请注意,我不会费心检查手表是否可以到达,因为如果手表无法到达(或者手表开始可到达并且在检查之后但在传输完成之前移出了范围),那么当手表到达时,数据仍然会发送出去从transferUserInfo返回范围。

在watchOS 2上吗? 正如其他人建议的那样,您需要使用WatchConnectivity。 由于watchOS 2进程不再像在watchOS 1中那样位于电话上,因此Darwin通知将不起作用。此外,WatchConnectivity更加方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM