如何在iOS Swift中隐藏Firebase推送通知的前台和后台?

时间:2019-01-09 10:46:29

标签: firebase push-notification firebase-cloud-messaging swift4

我正在使用Firebase推送通知iOS应用,但是在某些情况下,我们不必向用户显示推送通知,只有我必须使用随推送通知一起收到的数据。

从服务器发送的有效载荷

{ 
to: iosTokenList[i],
  notification: {
   title: 'Hidden',
   body: "Hidden",
   Data: {
   "Id":Id,
   "someList":SomeList,
   "status": "inactive"
    },
  notificationType: "status"
},

问题

当我不发送notification.titlenotification.body时,则在ios应用中未收到推送通知。在appDelegate中没有方法被调用,

但是当我们添加notification.title=somevaluenotification.body=somevalue时,willPresent被称为。

AppDelegate

@available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        NotificationParser.parseIncommingMessages(notification: notification)
        completionHandler([.alert, .sound])
    }
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        print("PushNotifications :  \(userInfo)")

        if #available(iOS 10.0, *){
        }else{
            let dict = userInfo as NSDictionary
            if let alert = dict.value(forKeyPath: "aps.alert") as? String{
                let alert = UIAlertController(title: appTitle  , message: alert + "didReceiveRemoteNotification", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
                appDel.window?.rootViewController?.present(alert, animated: true, completion: nil)
            }
        }
    }

0 个答案:

没有答案