从 Apple Watch 触发通知
Trigger Notification from Apple Watch
我正在尝试安排在按下 Apple Watch 上的按钮几分钟后触发本地通知(我正在实际设备上进行测试)。这就是我尝试的方式:
- (IBAction)buttonPressed {
[WKInterfaceController openParentApplication: nil reply: nil {
}
在 AppDelegate 中:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
}
问题是,我不知道将通知代码放在哪里。我很确定我必须调用 openParentApplication 来安排它,但不知道如何。我一直在网上阅读,但没有发现任何有用的东西。
好的,您快完成了:
首先这样调用:
- (IBAction)buttonPressed {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]
[dictionary setObject:@"notification" forKey:@"action"];
[MainInterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
//handle the reply if you want to...
}
}
然后在你的 AppDelegate 中:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
if([[userInfo objectForKey:@"action"] isEqualToString:@"notification"] ){
//fire the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// you can use Reply(Dictinary); for responding to watchKit extention OK or KO...
}
不要忘记在您的 appDelegate 中请求通知权限:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
return YES;
}
我正在尝试安排在按下 Apple Watch 上的按钮几分钟后触发本地通知(我正在实际设备上进行测试)。这就是我尝试的方式:
- (IBAction)buttonPressed {
[WKInterfaceController openParentApplication: nil reply: nil {
}
在 AppDelegate 中:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
}
问题是,我不知道将通知代码放在哪里。我很确定我必须调用 openParentApplication 来安排它,但不知道如何。我一直在网上阅读,但没有发现任何有用的东西。
好的,您快完成了:
首先这样调用:
- (IBAction)buttonPressed {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]
[dictionary setObject:@"notification" forKey:@"action"];
[MainInterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
//handle the reply if you want to...
}
}
然后在你的 AppDelegate 中:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
if([[userInfo objectForKey:@"action"] isEqualToString:@"notification"] ){
//fire the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// you can use Reply(Dictinary); for responding to watchKit extention OK or KO...
}
不要忘记在您的 appDelegate 中请求通知权限:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
return YES;
}