如何重定向到特定页面以进行推送通知?

时间:2018-08-20 06:49:52

标签: ios apple-push-notifications swift4

我正在处理ios推送通知,需要在点击它时将其重定向到特定页面。 如何实现此功能?

2 个答案:

答案 0 :(得分:0)

exports.showMyListingPage = async function(req, res) {
    let properties = await Property.find({}).populate({path: 'user',
  model: 'User',}).exec();
    console.log(properties[0].user);
}

答案 1 :(得分:0)

要在收到通知时重定向到特定页面,您必须首先处理收到的推送通知,

要处理收到的推送通知,请执行以下步骤:

  1. didFinishLaunchingWithOptions中将消息传递委托设置为self
  2. 注册以进行远程通知。这会在首次运行时显示一个权限对话框
  3. 您需要处理iOS 10之前的通知注册
  4. 实施didReceiveRemoteNotification以开始接收消息传递,在这里您可以处理接收到的数据,对其进行解析并重定向...当为运行iOS 10以下的iOS的设备点击通知时,将调用此方法
  5. 对于iOS 10及更高版本,您可以遵循UNUserNotificationCenterDelegate,并实现didReceive方法。就是这样

示例:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


        // [START set_messaging_delegate]
        Messaging.messaging().delegate = self
        // [END set_messaging_delegate]

        // Register for remote notifications. This shows a permission dialog on first run, to
        // show the dialog at a more appropriate time move this registration accordingly.
        // [START register_for_notifications]
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        return true
    }

处理收到的消息:

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // 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 full message.
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

处理iOS 10及更高版本收到的通知:

@available(iOS 10, *)
extension AppDelegate : 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

        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        // Change this to your preferred presentation option
        completionHandler([.alert, .badge, .sound])
    }

    //When clicked
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
        //You can here parse it, and redirect.
        completionHandler()
    }
}