[英]Is there a way to prevent users from receiving push notifications after they have been sent from Firebase?
我想知道我是否可以通過Firebase向用戶發送多個推送通知,但在用戶在設備上看到它們之前攔截它們並允許我的應用選擇性地允許通知。
這可能嗎?
如果是這樣,我需要使用哪種方法? (來自react-native-firebase
或iOS
)
新
您可以向用戶發送靜默通知,並將實際通知安排為本地通知
請參閱此
舊
(在Firebase的情況下)通知僅在應用程序位於前台時才可管理。 如果應用程序處於后台,則無法處理,因為用戶將收到通知,並且在通知被點擊之前不會通知應用程序。
您可以嘗試使用UNNotificationServiceExtension
(可從iOS 10
)。 您需要為項目添加擴展( 示例如何添加 )。
實現方法之后- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
在你的擴展名中。
它會在通知顯示給用戶之前調用,您可以嘗試執行某些操作(例如:將一些數據保存到本地)。
請注意 ,它僅適用於iOS 10 rich notifications
( 如何使用Firebase實現它 )。
PS希望對你有所幫助!
編輯 :
在Apple文檔中寫道:
重寫此方法並使用它來修改隨通知一起提供的UNNotificationContent對象。
更新 :
您可以嘗試從服務器靜默推送通知發送,檢查並創建本地通知,以便在需要時向用戶顯示。
當應用程序處於前台狀態時,可以處理FireBase通知。 首先,您在控制器和info.plist中都有來自Always的更改通知。 設置后,您可以在App委托或viewController中控制通知。
您可以發送將在前台和后台接收的數據通知。 在iOS中,它們由委托方法application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
處理,在處理之后,您可以決定是否為用戶創建推送。
@Update
用於在低於10的iOS上接收數據消息(不推送)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handleNotification(data: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
在iOS10上
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
handleNotification(data: remoteMessage.appData)
}
然后我只檢查自定義字段是否包含我需要的數據,並使用NotificationCenter創建本地通知
fileprivate func handleNotification(data: [AnyHashable : Any]) {
guard let topic = data["topic"] as? String else { return }
if data["receiver"] as? String == UserManager.shared.current(Profile.self)?.uuid && topic == "coupons" {
displayNotification(data: data)
}
}
fileprivate func displayNotification(data: [AnyHashable : Any]) {
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.title = data["notification_title"] as! String
content.body = data["notification_body"] as! String
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
let request = UNNotificationRequest.init(identifier: data["topic"] as! String, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
if currentNotification != nil {
UIApplication.shared.cancelLocalNotification(currentNotification!)
}
let notification = UILocalNotification()
notification.fireDate = Date()
notification.category = data["topic"] as? String
notification.alertBody = data["notification_body"] as? String
if #available(iOS 8.2, *) {
notification.alertTitle = data["notification_title"] as? String
}
currentNotification = notification
UIApplication.shared.scheduleLocalNotification(currentNotification!)
}
}
請記住,這適用於數據通知而非推送通知,但在Firebase中,您可以發送這兩種類型。
發送帶有"content-available" : 1
的靜音遠程通知"content-available" : 1
並決定是否要將其顯示給用戶。
{
"aps" = {
"content-available" : 1,
"sound" : ""
};
// add custom key value for message
}
如果要將其顯示給用戶,請為消息提取自定義鍵值對並觸發本地通知以供用戶顯示。 它也可以在后台運行。 需要為遠程通知啟用UIBackgroundMode
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.