2017-05-05 19 views
3

Firebase admin SDKを使用して自分のノードサーバーからプッシュ通知を送信しています。しかし、iOS上では、アプリケーションがバックグラウンド/終了しているときに通知を表示したいだけで、フォアグラウンドでは通知しない。現在、常に通知を表示します。フォアグラウンドで通知を表示しない

これは私のペイロードである:

const payload = { 
    data: { 
    data: 'data', 
    more: 'moreData', 
    }, 
    notification: { 
    title: 'Incoming Call', 
    body: 'Someone is calling you', 
    text: 'This is some text', 
    sound: 'default', 
    click_action: 'com.example.INCOMING_CALL', 
    } 
}; 
const options = { 
    priority: 'high', 
    time_to_live: 30, 
    collapse_key: 'Video Call', 
    content_available: true, 
}; 

admin.messaging().sendToDevice(tokensList, payload, options); 

は、それは私のペイロードを持つものですか、それは私がAppDelegate.swiftにしなければならないものですか? iOS8で

あなたは applicationStateを使用して AppDelegateでこれを達成することができます

答えて

6

:iOS10で

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 

    if !UIApplication.shared.applicationState == .active { 
    // Handle your push notification here 
    } 

} 

  1. import UserNotifications枠組み
  2. UNUserNotificationCenterDelegate 方法

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    
    
    if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing. 
        completionHandler([]) 
    } else { // If app is not active you can show banner, sound and badge. 
        completionHandler([.alert, .badge, .sound]) 
    } 
    
    } 
    

感謝を実装します。

2

iOSでは、AppDelegate.swiftで通知をどう処理するかを決定します。 AppDelegateでは、適切なケースの通知を処理し、アプリケーションがフォアグラウンドにあるときは破棄します。

iOSの10で

、アプリが終了したときに通知を処理するために、あなたは、通知からアプリを起動フォアグラウンドに通知を処理するために

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil { 
    if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary 

    } 
} 

} 

でそれを処理、

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    //handle notification here 

} 

この方法を使用します背景の通知を処理するには、この方法を使用します。

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

} 
関連する問題