2016-12-21 6 views
1

iOS 10.2でローカル通知を正常に実装しました。iOS 10.2でローカル通知が行われない

しかし、問題は、アプリがフォアグラウンドである場合は、アプリがバックグラウンドになっても警告が表示されないことです。

フォアグラウンドでローカル通知を受け取ることはできますか?アプリがフォアグラウンドで実行されている場合

私のコードはここに

func notificationNow(){ 

     print("notification will be triggered in five seconds..Hold on tight") 
     let content = UNMutableNotificationContent() 
     content.title = "Intro to Notifications" 
     content.subtitle = "Lets code,Talk is cheap" 
     content.body = "Sample code from WWDC" 
     content.sound = UNNotificationSound.default() 

     //To Present image in notification 
     if let path = Bundle.main.path(forResource: "menu2", ofType: "png") { 
      let url = URL(fileURLWithPath: path) 

      do { 
       let attachment = try UNNotificationAttachment(identifier: "sampleImage", url: url, options: nil) 
       content.attachments = [attachment] 
      } catch { 
       print("attachment not found.") 
      } 
     } 

     // Deliver the notification in five seconds. 
     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false) 
     let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger) 

     UNUserNotificationCenter.current().delegate = self 
     UNUserNotificationCenter.current().add(request){(error) in 

      if (error != nil){ 
       print(error?.localizedDescription as Any) 
      } 
     } 
    } 

答えて

0

書き込みあなたはアプリがバックグラウンドで動作しているときに、通知にタップフォアグラウンドまたはユーザーに通知を受信したときにこれが通知されますローカル通知

を観察したい場所クラスのコード(拡張子)を以下に示します。

これがあなたの問題を解決することを願っています。

UNUserNotificationCenterDelegateのドキュメントで
extension ViewController:UNUserNotificationCenterDelegate{ 


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    print("Tapped in notification") 
} 

//This is key callback to present notification while the app is in foreground 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

    print("Notification being triggered") 
    //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10 
    //to distinguish between notifications 
//  if notification.request.identifier == requestIdentifier 
//  { 

     completionHandler([.alert,.sound,.badge]) 

//  } 
     } 

} 
+0

で、ViewControllerを書いていました。私のCustomViewControllerの名前ではなく、唯一の問題です。 –

+1

自分のプロジェクトからコードを追加しました。 "ViewController.swift"内のそのプロジェクト拡張コードで、同じコードがコピーされました。 "ViewController"は、拡張機能が書き込む予定のView Controller名に置き換えてください。 – miOS

0

です。代理メソッドを使用してローカル通知をキャプチャする必要があります。そうで

あなたAppDelegatedidFinishLaunchingWithOptions方法では、デリゲートのリスニングを実装:

あなたのアプリが起動し終わる前に、デリゲートを設定することが重要です。

// Do NOT forget to retain your delegate somewhere 
let notificationDelegate = UYLNotificationDelegate() 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    let center = UNUserNotificationCenter.current() 
    center.delegate = notificationDelegate 

    // ... 
    return true 
} 

あなたは実用的な通知に応答したり、アプリがあなたがUNUserNotificationCenterDelegateを実装する必要がフォアグラウンドにあるときに通知を受信したい場合。このプロトコルは、2つのオプションメソッドを定義:

  • userNotificationCenter(_:willPresent:withCompletionHandler:)通知がフォアグラウンドアプリケーションに送達されるときに呼び出さ
    あります。あなた
    UNNotificationオリジナルを含むオブジェクト
    UNNotificationRequestを受信します。提示したい
    UNNotificationPresentationOptionsで完了ハンドラを呼び出します(.none to
    はアラートを無視します)。 ユーザが配信通知に作用を選択した場合

  • userNotificationCenter(_:didReceive:withCompletionHandler:)が 呼ばれます。 actionIdentifierとUNNotificationオブジェクトを含むUNNotificationResponseオブジェクトを受け取ります。 システム定義の識別子UNNotificationDefaultActionIdentifierUNNotificationDismissActionIdentifierは、ユーザーが 通知をタップしてアプリを開くか、スワイプして 通知を閉じたときに使用されます。

いずれの場合も、完了したら補完ハンドラを呼び出す必要があります。

#pragma mark - UNUserNotificationCenterDelegate Methods 

func userNotificationCenter(_ center: UNUserNotificationCenter, 
    willPresent notification: UNNotification, 
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
// Play sound and show alert to the user 
    completionHandler([.alert,.sound]) 
} 
+0

OPには「swift3」というタグが付いています。あなたはswift3で答えを書くべきです! – Lion

+0

@Lion:迅速なコードで更新。拡張子は – Arasuvel

-1

フォアグラウンドで表示するように通知を設定するための方法を以下の利用、

​​

あなたが詳細についてはApple documentationを参照することができます!

1

:あなたのアプリが起動し終わる前に、遅くともUNUserNotificationCenterオブジェクトにデリゲートオブジェクトを割り当てる必要がありません

重要

。たとえば、iOSアプリケーションでは、アプリケーション(:willFinishLaunchingWithOptions :)またはアプリケーション(:didFinishLaunchingWithOptions :)メソッドで割り当てる必要があります。

通知センターに通知が追加される直前に、後でその委任を設定するように見えます。

UNUserNotificationCenterDelegateに準拠する拡張子を持つシンプルなSwiftクラスシングルトンを作成しました。シングルトンのinitメソッドでは、デリゲートを自分に割り当てました。次に、AppDelegateのwillFinishLaunchingWithOptionsメソッドでシングルトンを初期化します。

関連する問題