fyno-push-ios 2.1.0

fyno-push-ios 2.1.0

Viram Jain维护。



 
依赖项
FMDB~> 2.7.5
SwiftyJSON~> 5.0
Firebase/Core~> 8.0
Firebase/Messaging~> 8.0
 

  • Viram Jain

Fyno iOS SDK

Fyno iOS SDK允许您在iOS应用程序中利用Fyno的服务,提供管理远程通知等功能的工具。以下是使用Swift将Fyno SDK集成到原生iOS应用程序中的说明。

要求

  • Fyno账户
  • 在设置>密钥和ID中可用的Fyno App ID
  • 用于测试的iOS 14+或iPadOS 14+设备(iPhone、iPad、iPod Touch)。Xcode 14+模拟器运行iOS 16+也有效。
  • Mac和Xcode 12+
  • p8身份验证令牌
  • 您的Xcode项目应针对任何Apple设备,除了Mac

安装

步骤 1:添加通知服务扩展

FynoNotificationServiceExtension 允许您的 iOS 应用接收带图像、按钮、徽章和其他功能的丰富通知。它对 Fyno 的分析功能也非常重要。

  1. 在 Xcode 中,选择 文件 > 新建 > 目标...
  2. 选择 通知服务扩展,然后按 下一步

Alt text

  1. 将产品名称输入为 FynoNotificationServiceExtension 并按 完成

Alt text

  1. 不要选择即将出现的对话框中的 激活

Alt text

  1. 激活方案 提示中按 取消。此步骤使 Xcode 继续调试您的应用,而不是您刚刚创建的扩展。如果您意外激活了它,可以在 Xcode 中切换回调试您的应用(在播放按钮旁边)。

Alt text

  1. 在项目导航器中,选择顶级项目目录,然后在项目目标和列表中选择 FynoNotificationServiceExtension 目标。
  2. 确保部署目标与主应用程序目标相同。它应至少设置为 iOS 10,这是苹果发布推送的丰富媒体的 iOS 版本。低于 iOS 10 的版本将不支持丰富媒体。
  3. 在项目导航器中,单击 FynoNotificationServiceExtension 文件夹,然后打开 NotificationService.mNotificationService.swift 并用提供的代码替换整个文件的内容。现在忽略任何构建错误。我们将导入 Fyno 以解决这些错误。
import UserNotifications
import fyno

class NotificationService: UNNotificationServiceExtension {
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        fynoService.handleDidReceive(request, withContentHandler: contentHandler)
    }

    override func serviceExtensionTimeWillExpire() {
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

此代码表示一个 'NotificationService' 类,它是 'UNNotificationServiceExtension'。'UNNotificationServiceExtension' 用于在通知显示给用户之前拦截和修改传入的远程推送通知。这对于需要向通知添加丰富内容(如媒体附件)或解码加密通知内容特别有用。

步骤 2:将 Fyno SDK 导入到您的 Xcode 项目

  • Fyno SDK 可以作为 Swift 包(与 Objective-C 兼容)添加。查看如何使用 Swift 包管理器从 Xcode 直接导入 SDK 的说明。
  • 将 Fyno SDK 添加到 Fyno 扩展服务中,以便 Fyno SDK 能够通过服务扩展处理和处理后台/丰富推送通知。

Alt text

步骤 3:添加必须的功能

此步骤确保您的项目可以接收远程通知。请只对这些步骤应用于主应用程序目标,不要应用于通知服务扩展。

  1. 选择根项目 > 您的主要应用程序目标并“签名和功能”。
  2. 如果您没有看到已启用 推送通知,请点击 + 功能 并添加 推送通知

Alt text 3. 点击 + 功能 并添加 后台模式。然后选择 远程通知Alt text

步骤 4:添加 Fyno 初始化代码

直接实现

导航到您的 AppDelegate 文件并添加 Fyno 初始化代码。

import Foundation
import UIKit
import fyno

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate  {
    let fynosdk  =  fyno.app

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        fynosdk.requestNotificationAuthorization { granted in
            if granted {
                DispatchQueue.main.async {
                    self.fynosdk.registerForRemoteNotifications()
                }
            }
        }
       fynosdk.enableTestMode(testEnabled: false)
       return true
    }


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        fynosdk.handleRemoteNotification(userInfo: userInfo, fetchCompletionHandler: completionHandler)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications: \(error.localizedDescription)")
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Send the device token to fynoServer
        let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
        
        fynosdk.initializeApp(WSID: YOUR_WORKSPACE_ID,api_key: YOUR_API_KEY, integrationID: YOUR_INTEGRATION_ID, deviceToken: token){
                    result in
                    switch result{
                    case .success(_):
                        self.fynosdk.createUserProfile(distinctID: "your_database_unique_identifier",name: "John Doe"){result in
                                            switch result{
                                            case .success(let success):
                                            print(success)
                                            case .failure(let error):
                                            print(error)
                                            }
                                        }
                        
                    case .failure(let error):
                        print(error)
                         
                    }
                }
            }
  
  // DELETE USER PROFILE
  //SIGNOUT
  /**
  fynosdk.deleteProfile(name: anonymous_profile_name){result in
                switch result{
                case .success(let success):
                    print(success)
                case .failure(let error):
                    print(error)
                }
            }
  **/
  
    
}

Fyno iOS SDK 实现步骤指南

Fyno iOS SDK 允许您在 iOS 应用程序中利用 Fyno 的服务。它提供了一组工具来处理远程通知以及其他功能。

安装

SDK 可以在以下 GitHub 仓库找到

https://github.com/fynoio/ios-sdk.git

初始设置
  1. 在您希望使用 Fyno SDK 的 Swift 文件中导入 UserNotificationsUIKit
import UserNotifications
import UIKit
  1. 初始化 Fyno 类的实例并将其设置为用户通知中心的代理。
let fynoInstance = fyno.app
UNUserNotificationCenter.current().delegate = fynoInstance
请求通知授权

使用 requestNotificationAuthorization 方法请求用户的通知权限。此函数接受一个闭包,该闭包接受一个布尔参数,指示是否授予权限。

fynoInstance.requestNotificationAuthorization { (granted) in
    if granted {
        // Permission granted
    } else {
        // Permission not granted
    }
}
注册远程通知

使用 registerForRemoteNotifications 函数将应用注册为接收远程通知。

fynoInstance.registerForRemoteNotifications()
处理通知

SDK 提供了几个方法来处理通知。

  1. handleRemoteNotification:此方法用于处理远程通知。它接受通知的用户信息和完成处理程序。
fynoInstance.handleRemoteNotification(userInfo: userInfo) { (fetchResult) in
    // Handle fetch result
}
  1. userNotificationCenter(_:willPresent:withCompletionHandler:):当应用处于活动状态时收到通知时会调用此方法。
fynoInstance.userNotificationCenter(center, willPresent: notification) { (options) in
    // Handle presentation options
}
  1. userNotificationCenter(_:didReceive:withCompletionHandler:):当用户与通知交互,例如点击或删除它时,会调用此方法。
fynoInstance.userNotificationCenter(center, didReceive: response) { 
    // Handle user response
}
实用工具

SDK 还提供了一个包含以下静态方法的实用工具类:

  1. downloadImageAndAttachToContent(from:content:completion:):此方法从 URL 下载图像,将其附加到 UNMutableNotificationContent 实例,并通过完成处理程序调用更新后的内容。

  2. createUserProfile(payload:completionHandler:):此方法使用 Payload 实例创建用户资料,并向服务器发送 POST 请求。它通过完成处理程序返回结果。

步骤 5:初始化/连接 Fyno SDK

此函数对于连接我们的 Fyno 应用至关重要。在应用启动时创建配置之前,需要调用该函数以创建配置:

fynosdk.initializeApp(WSID: WSID,api_key: api_key, integrationID: integrationID, deviceToken: AppDelegate.device_token){
            result in
            switch result{
            case .success(let success):
                print(success)
                CompletionHandler(.success(success))
            case .failure(let error):
                print(error)
                CompletionHandler(.failure(error))
            }
        }
    }

步骤 6:为定向创建用户配置文件

createUserProfile(payload:completionHandler:) 方法使用 Payload 实例创建用户配置文件,并发送 POST 请求到服务器。它调用完成为返回结果。

fynosdk.createUserProfile(distinctID: (result?.user.email)!,name: (result?.user.displayName)!){result in
                    switch result{
                    case .success(let success):
                    print(success)
                    case .failure(let error):
                    print(error)
                    }
                }

步骤 7:删除用户配置文件

deleteProfile() 方法使用 Payload 实例删除现有配置文件,并发送 POST 请求到服务器。它调用完成为返回结果。

fynosdk.deleteProfile(name: currentUser){result in
                switch result{
                case .success(let success):
                    print(success)
                case .failure(let error):
                    print(error)
                }
            }

步骤 8:切换到测试环境

fynosdk.enableTestMode(testEnabled:) 方法允许用户在 Fyno 应用中从默认的 'live' 环境切换到 'test' 模式。

fynosdk.enableTestMode(testEnabled: true)

如何在iOS应用程序中实现Fyno Inapp

本指南提供了使用SwiftUI将Fyno Inapp服务集成到iOS应用程序中的分步说明。在此示例中,我们将使用一个名为demo-ios-sdk-1的简单应用程序。

实现Fyno Inapp的步骤

  1. 导入Fyno Inapp SDK:在您的SwiftUI文件中,通常在ContentView.swift中,在文件顶部导入Fyno Inapp SDK。这通过以下行完成:import fyno
import SwiftUI
import fyno
  1. 初始化Fyno Inapp实例:接下来,在您的视图结构(在这种情况下,struct ContentView: View)中,初始化Fyno Inapp实例。为此,您需要提供以下内容:

    • inappUserId:这是您的应用程序中用户的唯一标识符。
    • integrationToken:这是Fyno Inapp提供的用于集成目的的令牌。
struct ContentView: View {
    let fynoinapp = fynoInapp(inappUserId: "kchandawat-1", signature: "e214cf26-4b04-4be3-b0b7-dac2fd3dfa78")
    ...
}
  1. 使用Fyno Inapp实例:现在Fyno Inapp实例已设置,您可以在视图中使用它。在此示例中,我们将fynoinapp实例传递给一个NotificationList视图,该视图会处理应用程序内的通知。
var body: some View {
    NotificationList(inappManager: fynoinapp)
}
  1. 预览内容视图:您可以使用PreviewProvider协议预览内容视图。
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

故障排除

如果遇到问题,请参阅我们的iOS故障排除指南。在我们的GitHub仓库上尝试示例项目。如果您遇到困难,请直接联系支持或通过以下邮箱请求帮助:[email protected]。为了加快帮助速度,请提供以下信息:

  • 您的Fyno密钥
  • 问题的详细信息、日志和/或截图。