跳转到内容

Flutter SDK

请按照以下指南,填写 FlareLane 发送推送通知所需的认证凭据。

在终端中进入项目的根目录,运行以下命令:

$ flutter pub add flarelane_flutter:1.9.2

为了获得顺畅的推送通知授权体验,请将 compileSdkVersion 设置为至少 33

// ...
android {
compileSdkVersion 33
// ...
}
// ...

进入目标的 Signing & Capabilities,点击左上角的 + Capability,选择 Push Notifications 添加。

同时选择 Background Modes,并启用 Remote notifications

Xcode 的 Signing and Capabilities 选项卡中已启用 Push Notifications 和 Background Modes

在 iOS 上,需要创建 Notification Service Extension 才能附加图片等媒体内容。

在 Xcode 中,依次进入 File > New > Target,选择 Notification Service Extension

Xcode 新建 target 对话框中已选中 Notification Service Extension

为 Product Name 填写一个合适的名称。在本指南中,我们使用 FlareLaneNotificationServiceExtension

Xcode 新建 target 配置中以 FlareLaneNotificationServiceExtension 作为 product name

点击 Cancel,以免激活一个单独的 scheme。

Xcode 询问是否激活新 scheme 的对话框,已高亮 Cancel

接下来,将刚创建的 Notification Service Extension TargetMinimum Deployments 版本设置为与主应用目标的版本一致。

Xcode 中 Notification Service Extension 的 Minimum Deployments 设置

为了在应用与扩展之间同步数据,您需要配置一个 App Group。

进入目标的 Signing & Capabilities,点击左上角的 + Capability,选择 App Groups 添加。

添加一个名为 group.bundleID.flarelane 的组**,其中 bundleID 必须与您应用的 Bundle Identifier 一致。**

Xcode 的 Signing and Capabilities 中已添加 App Groups capability 和 flarelane 组

添加成功后,启用该 App Group。

Xcode 的 App Groups capability 中已启用 flarelane 组

同样地,在您先前创建的扩展中添加并启用同名的 App Group。

Xcode 扩展 target 的 App Groups capability 中已启用 flarelane 组

接下来,打开 ios 目录下的 Podfile,添加以下内容:

// Add the lines below at the bottom of the file.
// Set the target name to the Product Name of the extension you entered earlier.
target 'FlareLaneNotificationServiceExtension' do
pod 'FlareLane', '1.9.2'
end

运行 pod install 即可完成 SDK 的安装。

  1. 添加 application(:didRegisterForRemoteNotificationsWithDeviceToken:) 的处理方法
import FlareLane
class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 2
FlareLaneAppDelegate.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}

5-5. 添加 UNUserNotificationCenterDelegate 方法

Section titled “5-5. 添加 UNUserNotificationCenterDelegate 方法”
  1. 将 UNUserNotificationCenterDelegate 指定给 AppDelegate
  2. 添加 userNotificationCenter(:willPresent:withCompletionHandler:) 的处理方法
  3. 添加一个处理方法
import FlareLane
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 1
UNUserNotificationCenter.current().delegate = self
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// 2
FlareLaneNotificationCenter.shared.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
}
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// 3
FlareLaneNotificationCenter.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
}

5-6. 添加 UNNotificationServiceExtension 方法

Section titled “5-6. 添加 UNNotificationServiceExtension 方法”
  1. 添加 didReceive(:withContentHandler:) 的处理方法
  2. 添加 serviceExtensionTimeWillExpire() 的处理方法
import UserNotifications
import FlareLane
class NotificationService: UNNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// 1
// Branch so that only FlareLane's methods run for push notifications sent by FlareLane.
if FlareLaneNotificationServiceExtensionHelper.shared.isFlareLaneNotification(request) {
FlareLaneNotificationServiceExtensionHelper.shared.didReceive(request, withContentHandler: contentHandler)
} else {
// ...
}
}
override func serviceExtensionTimeWillExpire() {
// 2
FlareLaneNotificationServiceExtensionHelper.shared.serviceExtensionTimeWillExpire()
}
}

在您的 main.dart 文件中添加以下初始化代码。您可以在控制台的 [Project] 页面找到您的项目 ID。

import 'package:flarelane_flutter/flarelane_flutter.dart';
void main() {
// Make sure the initialization code runs after ensureInitialized().
WidgetsFlutterBinding.ensureInitialized();
// Add the initialization code below.
// To control when the notification permission prompt appears, set the second parameter to false and call .subscribe() at the appropriate time.
FlareLane.shared.initialize("PROJECT_ID", requestPermissionOnLaunch: true);
runApp(...);
}

应用安装后,FlareLane 中创建的设备是一个「匿名设备」。通过关联您单独管理的唯一用户 ID,您可以将 FlareLane 的设备与您自己的用户 ID 进行匹配。

**关联用户 ID 有诸多好处。**它能让您区分已注册和未注册用户,并随时按用户 ID 发送推送通知,因此我们建议在初次集成时就完成此设置。

通常,您会在用户注册或登录成功时,通过 setUserId 函数关联用户 ID。

FlareLane.shared.setUserId("USER_ID");
  • 默认情况下,FlareLane 会在点击推送通知时自动处理 https 链接和深度链接等 URL。如果您需要实现自定义的点击处理逻辑,请参阅禁用自动 URL 处理
<resources>
<!-- Change the notification color -->
<string name="flarelane_notification_accent_color">#BC0000</string>
</resources>
指南
移动 SDK 参考
Flutter SDK 版本说明