繁体   English   中英

向Apple Watch扩展程序发送通知

[英]Sending notification to Apple Watch extension

我有一个iPhone 8.2应用程序在后台模式下与蓝牙配件 通信 (我在功能选项卡上启用它)。 每当我从附件收到消息(在iPhone应用程序包中处理)时,我都想向Apple Watch扩展程序发送通知 (以便用户可以看到附件的更新状态)。

我怎样才能做到这一点?


其他子问题

  • 理想情况下,如果Apple Watch应用程序扩展程序处于后台模式,我还希望用户看到通知( 问题2:苹果手表扩展程序是否可以进入后台模式?)。
  • 我也不确定是否可以在不打开Apple Watch应用程序的情况下发送通知。 问题3:这可能吗?

您可以使用MMWormhole发送该通知。

你发送它使用:

[self.wormhole passMessageObject:@{@"titleString" : title} 
                      identifier:@"messageIdentifier"];

你收到它使用:

[self.wormhole listenForMessageWithIdentifier:@"messageIdentifier" 
 listener:^(id messageObject) {
    // Do Something
}];

请注意,虫洞使用应用程序组进行通信,因此您需要启用它。

MMWormhole使用的内容是CFNotificationCenterGetDarwinNotifyCenter ,您可以在此媒体帖子中获得更多相关信息。

至于子问题,恐怕我没有100%肯定,但我相信是的,你苹果手表扩展也适用于后台模式。 至于第三个问题,我不明白。

适用于iOS 9.0及更高版本的更新。

虽然MMWormhole也适用于watchOS 2,但最好在watchOS 2.0及更高版本上使用Apple的WatchConnectivity框架。 MMWormhole需要使用App Groups,而WatchConnectivity则不需要。 WatchConnectivity确实需要iOS 9.0及更高版本。

以下是如何将简单的String从iOS应用程序发送到WatchKit扩展的快速示例。 首先让我们设置一个帮助类。

class WatchConnectivityPhoneHelper : NSObject, WCSessionDelegate
{
    static let sharedInstance:WatchConnectivityPhoneHelper = WatchConnectivityPhoneHelper()

    let theSession:WCSession = WCSession.defaultSession()

    override init()
    {
        super.init()

        // Set the delegate of the WCSession
        theSession.delegate = self

        // Activate the session so that it will start receiving delegate callbacks
        theSession.activateSession()
    }

    // Used solely to initialize the singleton for the class, which calls the constructor and activates the event listeners
    func start()
    {

    }

    // Both apps must be active to send a message
    func sendStringToWatch(message:String, callback:[String : AnyObject] -> ())
    {
        // Send the message to the Watch
        theSession.sendMessage(["testString" : message], replyHandler:
        { (reply: [String : AnyObject]) -> Void in
            // Handle the reply here

            // Send the reply in the callback
            callback(reply)
        },
        errorHandler:
        { (error:NSError) -> Void in
            // Handle the error here

        })
    }

    // A message was sent by the Watch and received by the iOS app. This does NOT handle replies to messages sent from the iOS app
    func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
    {
        // Handle received message logic here

        if (message["testString"] != nil)
        {
            replyHandler(["testString" : "Received Message!"])
        }
    }
}

对于WatchKit扩展,这个帮助程序类几乎是相同的。 我已经命名了这个类WatchConnectivityExtensionHelper的WatchKit扩展版本。 我不会粘贴它,因为它再次与上面的帮助程序类完全相同。

用法

我们需要通过实例化单例辅助类来启动iOS和WatchKit Extension消息监听器。 我们需要做的就是调用一些函数或引用单例中的一些变量来初始化它。 否则,iOS或WatchKit Extension将发送消息,但另一个可能无法接收消息。

iOS - AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        // Start the event listener for communications between the iOS app and the Apple Watch
        WatchConnectivityPhoneHelper.sharedInstance().start()

        ...
    }

    ...
}

WatchKit扩展 - ExtensionDelegate.swift

class ExtensionDelegate: NSObject, WKExtensionDelegate
{
    func applicationDidFinishLaunching()
    {
        // Start the event listener for communications between the iOS app and the Apple Watch
        WatchConnectivityExtensionHelper.sharedInstance.start()

        ...
    }

    ...
}

然后在iOS应用中的任何地方,调用sendStringToWatchString发送到WatchKit扩展:

WatchConnectivityPhoneHelper.sharedInstance.sendStringToWatch("Test message!")
{ (reply:[String : AnyObject]) -> Void in
    if (reply["testString"] != nil)
    {
        let receivedString:String = reply["testString"] as! String

        print(receivedString)
    }
}

暂无
暂无

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

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