2015-10-14 47 views
1

我開發了一個蘋果手錶應用程序。我想要一個簡單的行爲:當我在配對手錶應用上按下iPhone上的按鈕時,我想要顯示通知。這可能嗎? 我使用手錶OS 2,我的手錶應用程序通過WatchConectivity與iPhone應用程序完美通信,反之亦然。 因此,使用WCSession我可以通知表應用程序,但我怎麼能顯示在通知風格控制器,我的意思是,因爲這iOS應用程序的手錶套件通知

enter image description here

我看到的控制器,從WKUserNotificationInterfaceController繼承存在

- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { 

我怎樣才能讓這個方法被調用?我試圖在手錶創建UILocalNotification

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
notification.category = @"myCategory"; 
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:8]; 
notification.alertBody = @"This is local notification!"; 
notification.timeZone = [NSTimeZone defaultTimeZone]; 
notification.soundName = UILocalNotificationDefaultSoundName; 
notification.applicationIconBadgeNumber = 10; 
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Hello! This is Local Notification!" forKey:@"Notification"]; 
notification.userInfo = infoDict; 
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

的問題是,在手錶不存在UIApplication的,所以我便無法安排的通知。我在iOS應用程序上做了同樣的事情,但在這種情況下,我收到了iPhone上的通知。 我不得不說,手錶應用程序中存在一個類別名稱爲「myCategory」的NotificationInterfaceController。

謝謝!

回答

2
  1. 在您的iPhone創建一個UILocalNotification

UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.category = @"myCategory"; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:8]; notification.alertBody = @"This is local notification!"; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.soundName = UILocalNotificationDefaultSoundName; notification.applicationIconBadgeNumber = 10; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Hello! This is Local Notification!" forKey:@"Notification"]; notification.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:notification];

  • 鎖定您的iPhone。

  • 通知出現在Apple Watch和iPhone上。

  • didReceiveLocalNotification on WatchApp被調用。

  • 要點:您必須鎖定iPhone。

    相關問題