How to trigger notifications using WatchKit - ios

In the WatchKit developer guide, there are instructions on how to render custom notifications and it also says:
Apps are not required to do anything to support notifications. The system provides a default notification interface that displays the alert message from the notification. However, apps can customize the notification interface and include custom graphics, content, and branding.
I can get local notifications to trigger banner alerts on the phone, but they don't happen on the WatchKit app. I have tried debugging using all of the targets:
iOS app
Watch app
Watch Glance
Watch custom notification
Even while the phone app is in the background, none seem to trigger when the local notification is received. I have also tried this by simulating a remote notification being received in the background.
I'm hoping I don't have to call the watchKit's controller local/remote notification handlers directly.

I'm hoping I don't have to call the watchKit's controller local/remote notification handlers directly.
No, you don't.
The answer is XCode doesn't support debugging Apple Watch and iPhone simultaneously for now. That's why you can't push a notification from iPhone simulator to Watch simulator, only payload test by *.apns files is supported for now.

Its explained in Notification Essentials that
iOS decides whether to display that notification on the iPhone or on Apple Watch .
So I dont think(as of now) there is anything that can be done from the app to trigger Notifications specifically in the watch.
Edit: It looks like theres a bug in 8.2 beta.
WatchKit Known Issues
When viewing a notification in iOS simulator, clicking on the notification icon or body does not launch the corresponding Watch app
Release notes

In order to test watch notifications, you must first create a new build scheme.
Duplicate your watch app scheme, and in the "Run" section, choose your custom notification as the executable.
Now you can run the notification scheme.
Inside the extensions group in your project, under Supporting Files is a file called PushNotificationPayload.json.
You can edit the payload file to try different notifications and categories.
Source

I only had two targets when I created the projects:
[app name]
[app name] Watch App
Editing the Watch App scheme allows me to select another executable -- notably there's the Glance and Notification options there, and they work for me. Just select it, close, and run. I haven't tried duplicating the targets/schemes so I don't have to edit them, but I don't think that would be an issue.
Edit: Actually, the SDK tells you to edit the scheme too.

Related

Trigger apple watch application launch from iPhone [duplicate]

I would like to give a hint to the user, that my iOS app supports the Apple Watch.
So I would like to link/open the Apple Watch companion app from within my iOS app, very much similar to opening the Settings App using ([[UIApplication sharedApplication] openURL: [NSURL URLWithString:UIApplicationOpenSettingsURLString]];)
This shall enable the user to directly navigate to the Watch companion app to setup my app for the watch.
I could not find any URL which would open the companion app in general or specific to a section of the companion app.
If direct linking is not supported, I am also interested in alternative approaches for this use case.
Thanks in advance!
EDIT:
I checked the WatchKitSettings Info.plist file in the simulator to see if it registers any URL schema, but it does not.
I guess it's not possible to programmatically launch the watch companion app from iOS. The opposite way would be possible: to launch the iOS app in the background upon receiving a message from the watch. See WWDC talk Introducing Watch Connectivity.
You could check WCSession.defaultSession().watchAppInstalled and ask the user to launch the app if it is true.
Previously, it was not possible to launch the WatchKit App from the iPhone app, but the iPhone app could be launched—only in the background—with the openParentApplication method.
As of WatchOS 2, however, it is now not possible for either app to cause the other to launch. Instead, there are new methods for queueing changes to be picked up when the other app is launched in the future.
One partial solution for your use case could be displaying a local notification which could be tapped to open the watch app. The primary flaw in such an approach is I do not believe there is a way to limit this notification to the Watch only, so it would also appear on the iPhone where if tapped it would bring the iPhone app to the foreground. Messy if implemented like this, and therefore not worth it I'd have thought, even if it could pass app store review.

Dynamic Notification Interface will only work if the Watch app \ extension was opened at least once from the watch

Currently there are 3 flavors of notifications in the Apple Watch:
Short - You can't customize these at all.
Static - You can customize these on the Storyboard.
Dynamic - Can customize UI elements with new data not coming on
the Notification payload.
Here's the problem (The project is using Watch OS 1), if the app was never opened from the Apple Watch the Static notification will be displayed instead of the Dynamic one and the log will show the error
Took too long to show custom notification. Falling back to static
I've stripped down the notifications code & ViewController to nothing but this:
-(void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler
{
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
And I was still getting this error.
I must clarify that after the app runs from the watch at least once every thing is working as expected and Dynamic Notification Interface are displayed fine.
Things i've double checked:
Watch battery is above 70%
This is the only code in my didReceiveRemoteNotification
WKUserNotificationInterfaceController subclass is connected to the
Dynamic interface Controller in my storyboard file
I've searched the Apple docs for something that verify this behavior and couldn't find anything related. At first I thought that something was not configured correctly with my app but then discovered that this is common in all Apple watch apps i've tried.
Steps to reproduce:
Download an Apple watch app that was never installed on your device and that uses Dynamic notifications. (I've tested this on Telegram and Outlook for example)
Don't open the app from the watch, only from your phone.
send your self a push notification.
The notification will arrive to your watch as Static notification and the Devices log will show the "Took too long to show custom notification. Falling back to static" error.
Open the app from the watch and send another notification. This will arrive as Dynamic.
After the Watch app is opened at least once the above is very hard to reproduce. Deleting the app from phone & watch and reinstalling everything still displayed Dynamic notifications as it should.
Is anyone experienced that?
Is there a way to display dynamic notifications without opening the app at least once on the watch?

How to programmatically open Apple Watch companion app from iOS app

I would like to give a hint to the user, that my iOS app supports the Apple Watch.
So I would like to link/open the Apple Watch companion app from within my iOS app, very much similar to opening the Settings App using ([[UIApplication sharedApplication] openURL: [NSURL URLWithString:UIApplicationOpenSettingsURLString]];)
This shall enable the user to directly navigate to the Watch companion app to setup my app for the watch.
I could not find any URL which would open the companion app in general or specific to a section of the companion app.
If direct linking is not supported, I am also interested in alternative approaches for this use case.
Thanks in advance!
EDIT:
I checked the WatchKitSettings Info.plist file in the simulator to see if it registers any URL schema, but it does not.
I guess it's not possible to programmatically launch the watch companion app from iOS. The opposite way would be possible: to launch the iOS app in the background upon receiving a message from the watch. See WWDC talk Introducing Watch Connectivity.
You could check WCSession.defaultSession().watchAppInstalled and ask the user to launch the app if it is true.
Previously, it was not possible to launch the WatchKit App from the iPhone app, but the iPhone app could be launched—only in the background—with the openParentApplication method.
As of WatchOS 2, however, it is now not possible for either app to cause the other to launch. Instead, there are new methods for queueing changes to be picked up when the other app is launched in the future.
One partial solution for your use case could be displaying a local notification which could be tapped to open the watch app. The primary flaw in such an approach is I do not believe there is a way to limit this notification to the Watch only, so it would also appear on the iPhone where if tapped it would bring the iPhone app to the foreground. Messy if implemented like this, and therefore not worth it I'd have thought, even if it could pass app store review.

Text reply after watchOS notification is failing only when the corresponding app is installed on the watch

I have an iOS9 app with a watchKit 1.0 Extension. Most functionality is working fine. One of the main features of the app is a private messaging feature, which is also working great.
I'm using UIUserNotificationActionBehaviorTextInput for iOS9+ devices so that when a user receives a notification on their watch, they will be able to use the built in reply feature.
This works great, but only if the watchKit app is NOT installed on the watch. Users are able to reply to a notification on their watch with canned responses, or dictate a response with no problems. Note, this does NOT launch the watchKit app - it just uses the built-in reply functionality of the watch.
When the watchKit app IS installed on the watch, the "Message" and "Dismiss" buttons appear as expected. But as soon as someone clicks the Message button, instead of being able to tap or dictate a response, it just immediately goes back to the watch face.
It is 100% reproducible using two separate sets of an iPhone 6 running iOS9.1 and Apple Watch running watchOS 2.0.1. Since notifications can't be tested in the simulator, this is a very difficult problem to troubleshoot.
I could really use some guidance in what steps to take next.

Connecting apple watch to parse notifications?

I want my parse notifications to show on the apple watch, and simply open the app on the iPhone when the okay button is tapped. How would I go about doing this? Thanks!
According apple:
"When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch."
So, you can't ensure that notification will be received on the watch.
Also, in case the notification is received on the applewatch, it is not possible to open the iPhone by pressing a Button (or anything). It does not exist such a method for that. The only way to open the app is manually.

Resources