2016-11-28 89 views
0

我有多個用戶登錄,每個用戶都可以在他的位置添加警報。此警報將儲存在Firebase數據庫中。如何從我的應用程序在Swift中檢索通知

問題

的問題是我應該怎麼做才能讓用戶得到通知,如果有在他的區域聲明的警報。

alerts Firebase node

users Firebase node

+1

,你能做的最好的事情就是用GeoFire API,您可以註冊以在某個半徑上收聽,並且每次有東西進入或離開您可以通知用戶的區域。不幸的是,我沒有時間寫一個例子或完整的答案,這就是爲什麼我寫了一個評論,而不是。您可以從firebase團隊閱讀此博客文章開始。 https://firebase.googleblog.com/2014/08/geofire-goes-mobile.html – sweepez

+0

我的問題是如何通知用戶。我收到該區域的所有警報,但我無法推送通知 –

+0

我明白了,這取決於您是希望僅在應用程序處於前景或背景或兩者都顯示警報時。對於應用程序處於前臺的情況,您可以使用nsusernotification,甚至可以使用開源彈出庫的子類,我喜歡CNPPopupController。因爲當應用程序在後臺或甚至最糟糕的應用程序退出,那是另一回事。我沒有處理過,但我會假設你需要某種服務器/服務來監聽數據庫上的更改,以便通過FCM啓動遠程通知,對不起,我沒有太多幫助,祝你好運。 – sweepez

回答

0

的IOS 9及以上使用方法:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print(userInfo) 
} 

在功能解析,做你需要這方面的數據是什麼。 在IOS 10+中的appdelegate頭和輸入 採用進口UserNotification這種方法

@available(iOS 10, *) 
extension HyberFirebaseMessagingDelegate : UNUserNotificationCenterDelegate { 
    // Receive displayed notifications for iOS 10 devices. 
    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           willPresent notification: UNNotification, 
           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
let userInfo = notification.request.content.userInfo 
print(userInfo)//print user info 
    } 
} 

extension HyberFirebaseMessagingDelegate : FIRMessagingDelegate { 
    // Receive data message on iOS 10 devices. 
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     print("%@", remoteMessage.appData) 
    } 
} 

更多的細節,你可以因爲使用火力地堡讀here

相關問題