Swift 极光推送(二):配置工程

这里我是通过 pod 的方式导入的极光,因为我用的电脑开发环境已经配置完了,这里就略过了,其实是我也不会。

1.pod 初始化

打开终端切换到工程目录,执行 pod init


20220816140257.jpg

2.配置工程下的 Podfile 文件,添加极光

20220816140510.jpg

3.回到终端,执行 pod install

20220816140825.jpg

4.打开XCode工程目录,因为使用的Swift的原因,我们需要一个Bridging Header 文件,应该是叫桥接文件吧

20220816142315.png

20220816142359.png

20220816142520.png

5.这样就会生成 Bridging Header 文件,那两个生成的oc文件没啥用可以删了,然后修改一下这个文件

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

6.添加极光代码,别忘了把 key 换一下

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate  ,JPUSHRegisterDelegate{
    
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
                    let userInfo = notification.request.content.userInfo
        
                    let request = notification.request // 收到推送的请求
                    let content = request.content // 收到推送的消息内容
        
                    let badge = content.badge // 推送消息的角标
                    let body = content.body   // 推送消息体
                    let sound = content.sound // 推送消息的声音
                    let subtitle = content.subtitle // 推送消息的副标题
                    let title = content.title // 推送消息的标题
                    print("wy________1" + title)
    }
    
    
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
                let userInfo = response.notification.request.content.userInfo
                let request = response.notification.request // 收到推送的请求
                let content = request.content // 收到推送的消息内容
        
                let badge = content.badge // 推送消息的角标
                let body = content.body   // 推送消息体
                let sound = content.sound // 推送消息的声音
                let subtitle = content.subtitle // 推送消息的副标题
                let title = content.title // 推送消息的标题
        
                print("wy________2" + title)
    }
    
    
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) {
        
    }
    
    func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) {
        
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
        // 推送代码
        if #available(iOS 10, *) {
            let entity = JPUSHRegisterEntity()
            entity.types = NSInteger(UNAuthorizationOptions.alert.rawValue) |
              NSInteger(UNAuthorizationOptions.sound.rawValue) |
              NSInteger(UNAuthorizationOptions.badge.rawValue)
            JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
            
          } else if #available(iOS 8, *) {
            // 可以自定义 categories
            JPUSHService.register(
              forRemoteNotificationTypes: UIUserNotificationType.badge.rawValue |
                UIUserNotificationType.sound.rawValue |
                UIUserNotificationType.alert.rawValue,
              categories: nil)
          } else {
            // ios 8 以前 categories 必须为nil
            JPUSHService.register(
              forRemoteNotificationTypes: UIRemoteNotificationType.badge.rawValue |
                UIRemoteNotificationType.sound.rawValue |
                UIRemoteNotificationType.alert.rawValue,
              categories: nil)
          }
        
换key
        JPUSHService.setup(withOption: launchOptions, appKey: " app key", channel: "App Store", apsForProduction: false)
        
        _ = NotificationCenter.default
        
        return true
    }

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("get the deviceToken  \(deviceToken)")
        NotificationCenter.default.post(name: Notification.Name(rawValue: "DidRegisterRemoteNotification"), object: deviceToken)
           JPUSHService.registerDeviceToken(deviceToken)
    }

}

8.配置一下 Signing&Capabilities

20220816160754.jpg

这样广播方式的推送就可以了,别名或者别的功能基本上还是能弄明白的

9.插个小问题,demo运行的时候竟然报错了

错误信息:lb: framework not found Pods_项目名

百度了一下说删除一下这个东西

20220816161409.jpg

但是我一个项目没删一个项目删了,然后都好用???

希望有知道大哥评论一下

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容