2017-11-14 6 views
1

プッシュ通知を自分のプロジェクトに統合しました。何らかの理由で、フォアグラウンドにいるときではなく、背景の気分になったときにバナーが表示されます。私は何かを忘れてしまったと確信しています。どんな助けでも大いに感謝します。PushNotificationバナーがiosで表示されない10.3

import UserNotifications 
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate { 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self //not sure this is appropriate 
     registerForPushNotifications() 
} 
func registerForPushNotifications() { 
     UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { 
      (granted, error) in 

      guard granted else { return } 
      self.getNotificationSettings() 
     } 
    } 
    func getNotificationSettings() { 
     UNUserNotificationCenter.current().getNotificationSettings { (settings) in 
      print("Notification settings: \(settings)") 
      guard settings.authorizationStatus == .authorized else { return } 
      DispatchQueue.main.async(execute: { 
      UIApplication.shared.registerForRemoteNotifications() 
      }) 
     } 
    } 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

     let tokenParts = deviceToken.map { data -> String in 
      return String(format: "%02.2hhx", data) 
     } 

     let token = tokenParts.joined() 
     if let valueInDB = UserDefaults.standard.value(forKey: "Permission_Granted") { 

     UserDefaults.standard.setValue(token, forKey: "PUSH_TOKEN") 
     UserDefaults.standard.synchronize() 
     print("Device Push Token: \(token)") 


    } 

これは、フォアグラウンドまたはバックグラウンドで実行されているにも関わらず、通知を受け取るたびに呼び出されます。したがって、バッチアイコンが更新されます。

func application(_ application: UIApplication, 
         didReceiveRemoteNotification userInfo: [AnyHashable : Any], 
         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 
     { 

      print("Recived: \(userInfo)") 

      completionHandler(.newData) 

      self.onPushRecieved = MP3ViewController().catchPushNotifications 
      let notification = JSON(userInfo) 
      print(notification) 

      let mediaId = (notification["media_id"]).stringValue 
      print(mediaId) 
      var badgeCount = 0 
      var pushAvailable = false 


      if let pushCount = UserDefaults.standard.value(forKey: "PUSH_COUNT"){ 
       badgeCount = pushCount as! Int 
       print(badgeCount) 
       badgeCount = badgeCount + 1 
       UIApplication.shared.applicationIconBadgeNumber = badgeCount 
      } 

      print(badgeCount) 
      UserDefaults.standard.setValue(badgeCount, forKey: "PUSH_COUNT") 
      UserDefaults.standard.synchronize() 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      print(isOnBackGround) 
      if isOnBackGround { 
       if mediaId != nil{ 
        DispatchQueue.main.async { 
         let mP3ViewController = storyboard.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController 
         mP3ViewController.media_ID = mediaId 
         self.navigationController?.pushViewController(mP3ViewController, animated:true) 
         self.isOnBackGround = false 

        } 

       } 
      }else{ 
       print("Running on foreground") 

      } 

     } 
    } 
+1

UNUserNotificationCenterDelegateプロトコルは、通知を受信し、アクションを処理するためのメソッドを定義します。だから、カスタムバナーを扱う方法は何ですか? – danu

+0

@danu 'UNUserNotificationCenterDelegate'の' delegate'メソッドはどこにありますか? – Mannopson

答えて

2

あなたは、例えば、以下の機能を実装することがあります。このデリゲートの

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    completionHandler([.badge, .alert, .sound]) 
} 

UNUserNotificationCenterDelegate

関連する問題