【问题标题】:Show fcm notification if meet a condition with swift如果满足 swift 条件,则显示 fcm 通知
【发布时间】:2019-01-15 00:05:51
【问题描述】:

我正在使用 firebase 推送通知,我的应用订阅了一个主题,一切都很好。但我想知道如果通过通知是否可以显示通知。这是我的场景:

local_user_id = 10

var payload = {
  notification: {
    title: "hi",
    body: "this is a notification",
    sound: "default"
  },
  data: {
    user_id: "1",
    message: "you should pay $3020.25"
  }
};

1) 控制用户是否登录 (true/false) 2)获取通知的消息数据并查看:

if (payload.data.user_id = local_user_id && is_login){
     show_notification()
  }

3) 显示通知

实际上我只有通知,没有更多,我是 firebase 新手,这是我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
                (granted, error) in
            }
            application.registerForRemoteNotifications()
            FirebaseApp.configure()
            return true
    }

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
            let dict = userInfo["aps"] as! NSDictionary
            let message = dict["alert"]
            print("response")
            print(message)
        }

我不知道该怎么做,这可能吗? 提前致谢

【问题讨论】:

    标签: ios swift firebase push-notification


    【解决方案1】:

    如果您试图在应用程序在前台运行时仅向用户显示通知,您需要让您的 AppDelegate 符合 UNUserNotificationCenterDelegate。这是因为当应用程序运行时,通知将呈现给UNUserNotificationCenter共享对象。

    extension AppDelegate: UNUserNotificationCenterDelegate {
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void {
    
            let content = notification.request.content
            if content.data["user_id"] == local_user_id && is_login {
                completionHandler(.alert)
            } else {
                completionHandler([])
            }
        }
    }
    

    您可能希望确保在此块中的某个点执行完成处理程序,因为这是执行通知呈现的处理程序。如果您想要静音通知,可以使用completionHandler([]) 使警报静音。完成处理程序的其他可能选项可用here

    【讨论】:

    • 条件怎么样?我怎样才能先得到消息,然后显示通知?
    • 我不确定我是否遵循您的要求。通知始终发送给用户,但您只想在他们登录时向他们显示?如果是这种情况,您可以在userNotificationCenter:willPresent 方法中检查条件,请参阅上面的编辑。
    • 当应用程序处于活动和后台和非活动状态时有效?因为当应用关闭时会出现通知
    • 当应用处于非活动状态并处于后台时,通知将通过func application:didReceiveRemoteNotification 方法而不是userNotificationCenter:willPresent 方法。
    • 那么,我应该将条件复制到该方法中吗?
    【解决方案2】:

    您无法控制应用程序中的显示隐藏通知。您可以在后端放置一些逻辑,是否应显示此通知。不过,我有以下可能的解决方法。

    • 使用静默推送。然后触发本地通知。注意:静音 push 并不总是可靠的。
    • 因此,只需在您的有效负载中包含 content-available: 1,如图所示 在下方获取静音通知。它将充当静音通知。
    • 还应在 Info.plist 中将 UIBackgroundModes 设置为
      remote-notification

    但它仅限于运行和后台模式。如果 content-available 在您的应用离线时设置为 0,您将无法接收或处理它

    【讨论】:

    • 好的,我知道这是不可能的。但是我在哪里可以处理接收消息或 UNUserNotificationCenterDelegate 中的数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2023-03-13
    • 2021-05-30
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2019-03-01
    相关资源
    最近更新 更多