2016-05-31 3 views
0

에있을 때 나는이 방법 푸시 알림 메시지를받을 수 있습니다표시 알림 경고 응용 프로그램 전경

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
} 

을 그러나 문제는 앱이 같은 방법으로들이받은되는 경고 알림을 표시하는 방법입니다 그것은 앱이 백그라운드에있을 때 표시됩니다.

그럴 수 있습니까?

답변

1

didReceiveRemoteNotification 당신은 전경 앱이 만 콜백을 얻을 때

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
     let state = UIApplication.sharedApplication().applicationState 
     if state == UIApplicationState.Active { 
      //show alert here your app is in foreground 
     } 
     else{ 
      //your app is in background 
     } 

    } 
+0

그래 난을 알고 있지만 배경에 앱이있을 때 같은 경고 알림을 표시하고 싶습니다. –

+0

버디는 앱이 백그라운드에 있고 활성화되어있을 때 로컬 알림을 만들어서 던져 버릴 수 있습니다. 원격 알림처럼 절대적으로 보입니다. 배지 카운트도 처리 할 수 ​​있지만 사용자 탭 당신이 didRecieveLocalNotification 메쏘드에서 제어권을 얻게 될 알림에 대해서 : –

+0

나는 벌써 그 일을했습니다, 나는 배너 경고를 얻지 못했습니다, 그냥 소리 나 경고없이 알림 센터에 표시됩니다 –

2

는, 아이폰 OS가 경고를 표시하지 않습니다 아래로 별도로 그들을 처리 할 수 ​​있습니다, 당신은 비록 배경 또는 전경 여부에 상관없이 트리거 이 경우, 당신은

당신은 이런 식으로 작업을 수행 할 수 있습니다 ... 스스로를해야 할 :

는 'PN 전경보기'에 대한 펜촉을 작성, 예를 들면 :

,

enter image description here

그리고 당신은 전경에 PN을 얻을 때, 당신은보기를 인스턴스화 할 수 있으며, 예를 들어, 애니메이션으로 UIWindow에 코드를 추가 :

 // init and configure your custom PN foreground view 
     let nib = UINib(nibName: self.nibName, bundle: NSBundle(forClass: PnForegroundView)) 
     pnForegroundView = nib.instantiateWithOwner(nil, options: nil)[0] as! PnForegroundView 
     pnForegroundView.setWidth(UIScreen.width) 
     pnForegroundView.setHeight(63) 
     pnForegroundView.title = <some title from notification> 
     pnForegroundView.image = <some image from notification> 

     // add the view to the key window 
     let window = UIApplication.sharedApplication().keyWindow! 
     window.addSubview(pnForegroundView!) 

     // Change window level to hide the status bar 
     window.windowLevel = UIWindowLevelStatusBar 

     // Show the PN foreground view with animation: 

     self.pnForegroundView!.setBottom(0) 
     self.changeWindowLevelToHideStatusBar() 
     UIView.animateWithDuration(0.2) { 
      self.pnForegroundView!.setBottom(self.pnForegroundView!.height) 
     } 

는 물론, 당신이 대리인을 설정해야 이보기에서는 사례 사용자가 알림을 클릭하고 사용자가 알림을 닫을 때 사용됩니다.

또한 자동 해고에 시간을 추가 할 수 있습니다.

마지막 일 - PnForegroundView을 제거 할 때, 당신은 더 나은 AppDelegate에 방법에 completionHandler 라인은 나를 위해 일한 것을 추가

2

상태 표시 줄을 표시하기위한, 값을 기본값으로하여 UIWindow 수준을 재설정 :

//Called when a notification is delivered to a foreground app. 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
print("User Info = ",notification.request.content.userInfo) 

completionHandler([.alert, .badge, .sound]) 
} 
관련 문제