温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

xcode怎么配置推送通知

发布时间:2026-10-31 19:54:38 来源:亿速云 阅读:88 作者:小樊 栏目:移动开发

在 Xcode 中配置 Apple 推送通知(APNs) 一般分为 证书/能力配置 和 代码集成 两部分。下面以 iOS App(Swift) 为例,按步骤说明。


一、开启 Push Notification 能力

  1. 打开 Xcode 工程
  2. 选中 Target → Signing & Capabilities
  3. 点击 + Capability
  4. 搜索并添加 Push Notifications
  5. 再添加 Background Modes
    • 勾选 ✅ Remote notifications

二、配置 App ID 与证书(Apple Developer)

1. 开启 App 的 Push 权限

  • 登录 Apple Developer
  • Certificates, Identifiers & Profiles → Identifiers
  • 找到你的 App ID
  • 勾选 ✅ Push Notifications
  • 保存

2. 生成推送证书(或 Key)

二选一:

方式 A:APNs 证书(传统)

  • 创建 Apple Push Services 证书
  • 下载 .cer
  • 双击安装到钥匙串
  • 导出 .p12

方式 B:APNs Auth Key(推荐)

  • 创建 Apple Push Notification Authentication Key (p8)
  • 下载后可用于所有 App

三、Xcode 代码配置(Swift)

1. 请求推送权限

import UIKit
import UserNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

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

        UNUserNotificationCenter.current().delegate = self

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            print("推送授权:", granted)
        }

        application.registerForRemoteNotifications()
        return true
    }
}

2. 获取 Device Token

func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print("Device Token:", token)
    // 发送给你的服务器
}

func application(
    _ application: UIApplication,
    didFailToRegisterForRemoteNotificationsWithError error: Error
) {
    print("注册失败:", error)
}

3. 处理推送消息

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        completionHandler([.banner, .sound, .badge])
    }

    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        print("点击推送:", response.notification.request.content.userInfo)
        completionHandler()
    }
}

四、测试推送(本地 / 远程)

本地测试推送

let content = UNMutableNotificationContent()
content.title = "测试"
content.body = "这是一条本地推送"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)

远程测试

  • 使用 APNs + 服务器
  • 或用工具:
    • Push Notification Debug Tool
    • Knuff
    • Postman + APNs API

五、常见问题

✅ 真机才能测试远程推送
✅ Device Token 会变
✅ 开发 / 生产环境 Token 不同
✅ Bundle ID 必须一致


如果你需要:

  • Flutter / React Native 配置
  • APNs 服务器端示例(Node / Java / Python)
  • 极光 / 个推 / 友盟推送

告诉我你的技术栈,我可以直接给你对应方案。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI
助
手