AppleWatch Development Introduction Application of notification in seven--watchosFirst, Introduction
In an iOS system, there are two types of notifications that are supported: local notifications and remote notifications. Local notifications are used for time-class notifications, remote, or push, and are used for some hint of dynamic information. Here is a summary of some of the knowledge related to notifications:
local push: http://my.oschina.net/u/2340880/blog/405491.
remote push: http://my.oschina.net/u/2340880/blog/413584.
In watch, notifications are synced with the iphone, and when the app on the iphone is notified, it is pushed to watch by default, and the notification message on the user's watch is more timely than the iphone.
Ii. Overview of watchOS Notifications
First, the notice on watch is divided into two parts: Short-look and Long-lock. In short, short-look, can be understood as a simple notification preview, it will be the notification of the launch of the app and the main title and other information to you, let you at a glance, when the user lifted the wrist, see this notice for a certain time, this short notice will be converted to Long-look notification. Short-look the notification interface we can not customize, the system for us to design the appearance, as follows:
Long notification interface We are able to do a certain degree of customization, and can add logic operations such as buttons.
Long-look is also divided into two interfaces, a static interface and a dynamic interface. This is well understood, the static interface is the interface we defined when writing the program, when the notification is sent to watch, the interface will automatically match the notification content to display. The dynamic interface is when the notification is received, the corresponding configuration code will be executed first, and then the display of the notification interface. A Long-look interface is roughly as follows:
In Long-lock, the interface is defined as three parts, a header title bar, a custom view bar, and a button interaction area. The title bar of the head we can not customize, it is a semi-transparent with the app icon and the name of the horizontal bar. Below are the areas we can customize, where we can pull text and pictures in the storyboard. At the bottom are some interactive buttons, whose names and other configuration information are defined in the push file.
Third, how to simulate remote push on the simulator
On the watchOS simulator, Xcode has prepared a way for us to simulate the test push. If Notifacationscene is selected when we create the project, Xcode creates a APNs file for us by default:
This file is the relevant configuration file for the simulation push, and if not, we can create it manually:
The contents in the file are formatted as follows:
{ "APS": { "alert": { "Body": "notifications", "title": "Notifications" }, "category": "MyCategory"     } , "Watchkit simulator actions": [ { " Title ": " First button ", " Identifier ": " Firstbuttonaction " } ], "Customkey": "use this file to define a testing payload for your notifications. the aps dictionary specifies the category, alert text and title. the watchkit simulator actions Array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. if you have multiple such Json files in your project, you ' Ll be able to select them when choosing to debug the notification interface of your Watch app. "}
This is some JSON-formatted data, where alert is the setting of the push content, the body will be displayed in the Long-look title bar, title will be displayed in the Short-look title bar, the actions array is the button on the line configuration, Each button can be set with a caption and ID, and the caption is used to display in the push interface, and the ID is used to handle the logic triggered by the click button.
Create this, we can try to test the push interface, select Push Project, run can:
Iv. static interface and dynamic interface of Long-look
As mentioned above, Long-look is divided into static interface and dynamic interface two, when we pull in storyboard a notification Interface controller, you can choose to create a dynamic interface at the same time, tick the has dynamic Interface:
At this time, in the storyboard is the following appearance:
We are creating a file, inheriting from Wkusernotificationinterfacecontroller, and setting the class of the dynamic push controller in storyboard to the classes we created:
Note that the dynamic interface is set here, which is the controller on the right. After that, you'll see that the effect doesn't change, because the system will load the push interface from the static interface by default, and we need to do something in the Notifacationcontroller code:
In Notificationcontroller, rewrite the following two methods//This is used for local push override func didreceivelocalnotification ( localnotification: uilocalnotification, withcompletion completionhandler: (( Wkusernotificationinterfacetype) ( -> void)) { Here do some dynamic interface loading operations, such as can be based on push data set image text //The following method determines whether to load a static interface or a dynamic interface / /custom is loading the dynamic interface //default is loading the static interface completionhandler (. Custom) } //set up a remote push Similar to the above method Override func didreceiveremotenotification (remotenotification: [nsobject : AnyObject], withCompletion completionHandler: ((Wkusernotificationinterfacetype) - > voID)) { Completionhandler (. Custom) }
V. Triggering a Push click event
First, we configure several click buttons, which are configured as follows in the APNs file:
"Watchkit simulator actions": [ { "title": "First", "identifier": "one" }, { "title": "second", "identifier": "both" }, { "title": "third", "identifier": "three" } ],
The following methods are implemented in the Interfacecontroller of our Watch app:
Override the following two methods to respond to a click event//Remote Push method override Func Handleactionwithidentifier (identifier:string?, forremotenotification Remotenotification: [Nsobject:anyobject]) {//through our configured button ID to differentiate the clicked button processing response logic print (identifier)}//local Push method O Verride func handleactionwithidentifier (identifier:string, forlocalnotification localnotification: uilocalnotification) {}
AppleWatch Development Introduction Application of notification in seven--watchos