返回

iOS Swift极光推送:点击消息推送内容跳转到对应界面

IOS

iOS Swift 中极光推送:点击消息推送后轻松跳转到指定界面

简介

极光推送作为行业领先的移动推送平台,提供一站式消息推送服务,助力移动应用实现高效、稳定的消息触达。本文将重点介绍在 iOS Swift 环境下使用极光推送时,如何实现点击消息推送后跳转到对应界面的功能。我们将提供详细的步骤指导、代码示例和常见问题解答,帮助开发者轻松掌握这一实用功能。

场景分析

当用户点击极光推送的消息时,我们需要根据应用的状态采取不同的处理方式:

  • 应用在后台: 点击消息推送后,应用将从后台唤醒并直接跳转到指定界面。
  • 应用已杀死: 点击消息推送后,应用将先启动,然后再跳转到指定界面。

实现方案

应用在后台

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // 处理极光推送消息
    JPUSHService.handleRemoteNotification(userInfo)
    
    // 获取自定义字段中的界面名称
    if let customField = userInfo["customField"] as? [String: Any],
       let interfaceName = customField["interfaceName"] as? String {
        // 根据界面名称跳转到指定界面
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let interfaceViewController = storyboard.instantiateViewController(withIdentifier: interfaceName)
        self.window?.rootViewController?.present(interfaceViewController, animated: true, completion: nil)
    }
    
    completionHandler(.newData)
}

应用已杀死

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 处理极光推送消息
    if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
        JPUSHService.handleRemoteNotification(remoteNotification)
        
        // 获取自定义字段中的界面名称
        if let customField = remoteNotification["customField"] as? [String: Any],
           let interfaceName = customField["interfaceName"] as? String {
            // 根据界面名称跳转到指定界面
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let interfaceViewController = storyboard.instantiateViewController(withIdentifier: interfaceName)
            self.window?.rootViewController?.present(interfaceViewController, animated: true, completion: nil)
        }
    }
    
    return true
}

示例代码

为了更好地理解如何实现上述功能,我们提供一个完整的示例代码:

// iOS Swift极光推送点击消息推送内容跳转到对应的界面示例代码

import UIKit
import JPush

// AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, JPUSHRegisterDelegate {
    
    // 极光推送SDK的初始化,只有初始化后才能正确接收和处理极光推送消息
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 初始化JPush
        JPUSHService.setup(withOption: launchOptions, appKey: "YOUR_APP_KEY", channel: "YOUR_CHANNEL", apsForProduction: false)
        
        // 设置极光推送接收处理的回调函数
        JPUSHService.setRegistrationIDDelegate(self)
        
        // 处理极光推送消息
        if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
            JPUSHService.handleRemoteNotification(remoteNotification)
            
            // 获取自定义字段中的界面名称
            if let customField = remoteNotification["customField"] as? [String: Any],
               let interfaceName = customField["interfaceName"] as? String {
                // 根据界面名称跳转到指定界面
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let interfaceViewController = storyboard.instantiateViewController(withIdentifier: interfaceName)
                self.window?.rootViewController?.present(interfaceViewController, animated: true, completion: nil)
            }
        }
        
        return true
    }
    
    // 接收到极光推送的registrationID,该registrationID用于标识当前设备的推送服务。一般在注册成功之后才会收到这个回调。
    func jpushNotificationRegistrationID(_ registrationID: String?) {
        if registrationID != nil {
            print("极光推送registrationID:\(registrationID!)")
        }
    }
    
    // 应用从后台唤醒后执行
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // 处理极光推送消息
        JPUSHService.handleRemoteNotification(userInfo)
        
        // 获取自定义字段中的界面名称
        if let customField = userInfo["customField"] as? [String: Any],
           let interfaceName = customField["interfaceName"] as? String {
            // 根据界面名称跳转到指定界面
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let interfaceViewController = storyboard.instantiateViewController(withIdentifier: interfaceName)
            self.window?.rootViewController?.present(interfaceViewController, animated: true, completion: nil)
        }
        
        completionHandler(.newData)
    }
}

// ViewController.swift
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 推送测试
        // 实际使用时,替换成自己的自定义参数
        let notification = ["customField": ["interfaceName": "DetailViewController"]]
        JPUSHService.sendRemoteNotification(notification)
    }
}

常见问题解答

  1. 如何获取极光推送的registrationID?
    registrationID是极光推送用来标识当前设备的推送服务。可以通过实现JPUSHRegisterDelegate中的jpushNotificationRegistrationID方法获取。

  2. 如何根据不同的应用状态处理点击消息推送?
    在应用在后台和应用已杀死的情况下,处理点击消息推送的方式不同。需要根据具体的应用状态实现不同的处理逻辑。

  3. 如何自定义消息推送内容?
    可以通过极光推送控制台或API自定义消息推送内容,包括标题、内容和自定义字段。

  4. 如何在点击消息推送后关闭极光推送通知?
    可以通过JPUSHService.setBadge(0)方法关闭极光推送通知。

  5. 如果点击消息推送后跳转到指定界面失败,可能的原因是什么?
    可能的原因包括:界面名称不正确、应用未正确配置极光推送SDK、推送消息中未包含自定义字段或网络连接问题。

结论

通过使用极光推送,开发者可以轻松实现点击消息推送后跳转到对应界面的功能,提升用户体验,打造更加实用的移动应用。本文提供了详细的步骤指导、代码示例和常见问题解答,帮助开发者快速上手这一功能。如果您在集成过程中遇到任何问题,欢迎随时联系极光技术支持团队寻求帮助。