如何从我的应用程序在Swift中检索通知

问题描述:

我有多个用户登录,每个用户都可以在他的位置添加警报。此警报将储存在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

的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