5

クリック時にUNUserNotificationCenter関数が呼び出されないアクションボタンアプリケーションがアクティブでない場合に3Dタッチ後にチャット通知を行います。アプリケーションが終了したときに、Xcodeで「名前でプロセスにアタッチ」してアプリケーションをデバッグしました。ここでは、コードは次のとおりです。この機能が最初に呼び出される(MixPanelから送られた)通知ポップアップUNUserNotificationCenter didRecieveレスポンスは、アプリケーションが終了した場合に呼び出されません。

import UIKit 
import Mixpanel 
import UserNotifications 

@UIApplicationMain 

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     //setup mixpanel 

     self.handlePushNotificationWithUserInfo(launchOptions: launchOptions) 

     //ask for push notification perms 
     return true 
    } 

コール1:

func handlePushNotificationWithUserInfo(launchOptions: [NSObject: AnyObject]?) { 
     //Handle PushNotification when app is opened 
    } 

それは

、ここに行きます

コール2:

//register for notification 
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { 

     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in 
       // Enable or disable features based on authorization. 
      } 
      center.delegate = self 

      let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground]) 
      let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) 
      let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions) 
      center.setNotificationCategories([customerSupportCategory]) 
     } 

     application.registerForRemoteNotifications() 
    } 

コール3:機能以下

// remote notification 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     ....Some Code.... 
    } 

しかし呼び出されません。 アプリがバックグラウンドで実行されている場合以下の機能が呼び出され、はすべて正常に動作します。それ以外の場合は、アプリケーションはフォアグラウンドになり、チャットはになりません。

// action buttons in enhanced Notification 
    @available(iOS 10, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
     guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else { 
      completionHandler() 
      return 
     } 
     switch action { 
     case .CHAT: 
      _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) 
     default: 
      _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL) 
     } 
     completionHandler() 
    } 

    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
     completionHandler([.alert, .sound]) 
    } 
} 

この機能は、それがuserNotificationCenter上のiOS 10に減価償却ているため、)(わからないかもしれ呼び出されることはありません。私はデバッグ装置としてiPhoneのiOS 6秒10を使用しています

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
     ....Some Code.... 
    } 

...また、これを説明してください。次のようスウィフト3 &のXcode 8にローカル通知を受け XCodeの8ベータ3

+0

アプリが実行しているか、ユーザーとの通知によって殺されていないようなシナリオで、次に受信します適切に。 – user1140780

答えて

6

私自身の実験からは、以下のとおりです。

適合

  • UNUserNotificationCenterDelegate

    に準拠します
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... } 
    
  • 通知センターのデリゲートとして
  • 登録:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    
        let center = UNUserNotificationCenter.current() 
        center.delegate = self 
    
        return true 
    } 
    

デリゲートメソッド

  • が応答が通知を逃したために(例えば、通知が送信されたときにユーザー閲覧アプリ)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
        print(notification.request.content.userInfo) 
    } 
    

    アクション通知に応答します。ユーザーは、アプリが実行しているか、ユーザーとの通知によって殺されたが、このようなシナリオでは、その後受信されていない場合、あなたがdidFinishLaunchingWithOptionsで処理し、アプリが通知を経由して開かれ、適切に機能しているかどうかを確認する必要が

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
        print(response.notification.request.content.userInfo) 
    } 
    
+0

はい、実際に私はこれらすべての手順を完了しました。後で私の間違いを見つけましたが、まだ回答と謝辞を追加していないので、** RegisterNotification.sharedHandler.registerFor3dTouchInNotification(appDelegate:self)** ** ** func handlePushNotificationWithUserInfo launchOptions:[NSObject:AnyObject]?)**私たちが通知を受けるたびにカテゴリを登録しますが、正しい方法であるかどうかわかりませんが、私の問題を解決しました.. –

0

)の通知を開きました。

//通知

if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { 
     notificationReceived(notification) 
    } 
+0

'UNUserNotificationCenter'ではもう例外ではありません。 。 –

1

スウィフト3.1アップデート

  • から起動した場合UNUserNotificationCenterDelegateに準拠チェック

  • 登録通知センターのデリゲートとして:

あなたはdidFinishLaunchingWithOptionsで処理し、アプリが通知と行為を経由して開かれているかどうかを確認する必要があり
UNUserNotificationCenter.current().delegate = self 
  • デリゲートメソッド
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print(response.notification.request.content.categoryIdentifier) 
} 

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print(notification.request.content.categoryIdentifier) 
} 
関連する問題