2017-02-13 90 views
0

我已經使ExtensionDelegate UNUserNotificationCenterDelegate並正確地分配它。我有我的watchOS 3應用程序添加一個通知請求,這將觸發watchOS 3 - 我應該在userNotificationCenter中做什麼:willPresentNotification:withCompletionHandler:實際顯示通知?

userNotificationCenter:willPresentNotification:withCompletionHandler: 

我已經實現方法如下:

- (void) userNotificationCenter:(UNUserNotificationCenter*)center 
     willPresentNotification:(UNNotification*)notification 
      withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler 
{ 
    NSLog(@"ExtensionDelegate: willPresent!"); 

    completionHandler(UNNotificationPresentationOptionAlert); 
} 

的問題是,什麼也沒有發生。我在運行watchOS 3.1的設備上在Xcode 8.2中進行調試,雖然這個委託方法被觸發,並且我可以觸發斷點,但不顯示任何提示。我的內容如下:

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; 
    content.title = notificationType; 
    content.subtitle = [NSString stringWithFormat:@"Subtitle: %@", notificationType]; 
    content.body = alertTitle; 
    content.badge = @1; 
    content.sound = [UNNotificationSound defaultSound]; 
    content.launchImageName = @"LaunchImage"; 
    content.userInfo = @{ 
         @"notificationType" : notificationType, 
         @"count" : @(count) 
         }; 
    content.attachments = @[]; 
    content.categoryIdentifier = notificationType; 

其中notificationType是的NSString我在我的應用程序的啓動設置如下:

// Register custom notification types 
    UNNotificationCategory* cat1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" 
                      actions:@[dismissAction] 
                   intentIdentifiers:@[] 
                      options:UNNotificationCategoryOptionNone]; 

    UNNotificationCategory* cat2 = [UNNotificationCategory categoryWithIdentifier:@"Category2" 
                      actions:@[dismissAction, yesAction] 
                    intentIdentifiers:@[] 
                      options:UNNotificationCategoryOptionNone]; 
    NSSet* categorySet = [NSSet setWithArray:@[cat1, cat2]]; 
    [center setNotificationCategories:categorySet]; 

不幸的是,通知請求時,willPresentNotification來得快,去的completionHandler被調用,然後應用程序繼續在前臺運行。屏幕上不顯示通知提醒。它也不出現在錶盤的下拉式底座上。我還需要做些什麼來讓它出現,或者在屏幕上或者在屏幕上顯示,或者進入下拉式底座?

下面是我期望在應用程序頂部看到的視覺示例,或者在屏幕頂部與頂部對齊的應用程序,並且應用程序在其下面運行。

Simple watchOS Notification

+0

您是否申請了發送通知的權限?從手錶編程指南:系統可以發佈提醒或播放應用通知的聲音之前,Watch應用或iOS應用必須請求授權才能與用戶交互。您可以通過調用共享UNUserNotificationCenter對象的requestAuthorizationWithOptions:completionHandler:方法來執行此操作。 – lostAtSeaJoshua

+0

不,我在啓動時請求了許可,並在啓動時獲得了它。我只需要發送實際的警報本身。就像出現在手錶屏幕頂部的橫幅一樣,然後消失。我們是否必須將它創建爲WKInterfaceView(或其他)? – Cindeselia

+0

我不清楚的是,當代理人的willPresentNotification被調用時,我們應該怎麼做才能真正顯示通知?我不需要像改變用戶界面那樣華麗的自定義行爲 - 我只需要在手錶屏幕頂部顯示的小橫幅(一些設計師稱其爲「煎餅」或「敬酒」),應用程序圖標和一個單行文本。 「達成目標!」管他呢。例如,這是我會在鐘面上的向下滑動菜單中看到的那種通知;我只需要表明這一點。我必須從頭開始編碼嗎?似乎我們不是免費的。 – Cindeselia

回答

0

相關的問題,但對於iOS的:比出這裏的旗幟可以更薄Displaying a stock iOS notification banner when your app is open and in the foreground?

我一直在努力的手錶同樣的事情,得到相同的(非顯示的通知)結果。有一點需要注意的是,如果它能幫助其他人,那就是我們必須要求我們在完成處理程序中返回相同的許可。例如,如果我們想要顯示Alert通知,我們必須使用如下所示的兩個對應值。正如OP所述,我們必須將自己設置爲通知中心代表。

(順便說一句,如果我在這裏有任何錯誤,請指出 - 我的工作還沒有)。

以下代碼位於watchkit擴展委託中。

- (void)applicationDidFinishLaunching { 
    // ... 
    // Register for, and request permission to send notifications 
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; 
    center.delegate = self; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert) 
         completionHandler:^(BOOL granted, NSError * _Nullable error) { 
          // Enable or disable features based on authorization. 
          NSLog(@"Got auth response: %d", granted); 
         }]; 
} 

僅當我將通知權限設置爲「鏡像iPhone」時,上述操作才成功。如果我把它關閉,我的權限請求失敗。一個不同的問題,但請檢查返回的granted值,並確保它是true

// Foreground notification 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
     willPresentNotification:(UNNotification *)notification 
      withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { 
    // The method will be called on the delegate only if the 
    // application is in the foreground. 
    NSLog(@"Will present notification: %@", notification); 
    // The following doesn't present the notification, though. 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 

userNotificationCenter:willPresentNotification:withCompletionHandler方法不會被調用,但我沒有看到通知疊加,也不是有一個通知中的列表。

相關問題