如何在Flutter项目中集成Urban Airship?(支持Android/iOS双端)
Hey there! I totally get the dilemma—choosing between Xamarin and Flutter when you’re locked into using Urban Airship, especially since Xamarin has that polished official integration docs. But don’t worry, integrating Urban Airship with Flutter is totally doable for both platforms. Let’s break it down step by step:
1. Add the Flutter Plugin
First, include the official Urban Airship Flutter plugin in your project. Add this to your pubspec.yaml file:
dependencies: urban_airship_flutter: ^[replace_with_latest_version]
Run flutter pub get to install the plugin.
2. Android Configuration
a. Add Credentials & Autopilot
- Open
android/app/src/main/res/values/strings.xmland add your Urban Airship app key and secret:<string name="urbanairship_app_key">YOUR_UA_APP_KEY</string> <string name="urbanairship_app_secret">YOUR_UA_APP_SECRET</string> - Update your
android/app/src/main/AndroidManifest.xmlto reference these values and enable the Flutter Autopilot (add inside the<application>tag):<meta-data android:name="com.urbanairship.autopilot" android:value="com.urbanairship.flutter.FlutterAutopilot" /> <meta-data android:name="com.urbanairship.app_key" android:value="@string/urbanairship_app_key" /> <meta-data android:name="com.urbanairship.app_secret" android:value="@string/urbanairship_app_secret" />
b. Required Permissions
Add these permissions to the top of your AndroidManifest.xml (outside the <application> tag):
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
For Android 13+, use the permission_handler package to request the notification permission at runtime.
3. iOS Configuration
a. Update Info.plist
Open ios/Runner/Info.plist and add your Urban Airship credentials, notification permissions, and push settings:
<key>UAAppKey</key> <string>YOUR_UA_APP_KEY</string> <key>UAAppSecret</key> <string>YOUR_UA_APP_SECRET</string> <key>UAPushEnabledLaunchOption</key> <true/> <key>NSUserNotificationUsageDescription</key> <string>We use notifications to keep you updated on important app events.</string> <key>NSRemoteNotificationType</key> <integer>7</integer>
b. Initialize in AppDelegate
Update ios/Runner/AppDelegate.swift to initialize Urban Airship:
import UIKit import Flutter import UrbanAirship @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { // Initialize Urban Airship Airship.takeOff(launchOptions: launchOptions) GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
c. Enable Push Capabilities in Xcode
- Open your iOS project in Xcode.
- Go to your project’s Target > Signing & Capabilities.
- Click
+ Capabilityand add Push Notifications. - Add another capability: Background Modes, then check Remote notifications.
4. Flutter Layer Setup
a. Initialize Urban Airship
In your main.dart, initialize the SDK before running your app:
import 'package:flutter/material.dart'; import 'package:urban_airship_flutter/urban_airship_flutter.dart'; import 'package:permission_handler/permission_handler.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); // Initialize Urban Airship await UrbanAirship.initialize(); // Request notification permission if (await Permission.notification.request().isGranted) { await UrbanAirship.push.enableUserNotifications(); } runApp(const MyApp()); }
b. Handle Push Events
Listen for notification events in your widget:
@override void initState() { super.initState(); // Listen for notification opens UrbanAirship.push.notificationOpened.listen((event) { print("Notification opened: ${event.notification.title}"); // Add custom handling here }); // Listen for received notifications UrbanAirship.push.notificationReceived.listen((notification) { print("Notification received: ${notification.title}"); }); }
Key Notes to Remember
- Credentials: Ensure your app key and secret match the ones in your Urban Airship dashboard for both platforms.
- iOS Certificates: Generate and upload APNS certificates to Urban Airship to enable iOS push notifications.
- Testing: Always test on physical devices—emulators/simulators often don’t handle push correctly.
- Troubleshooting: If you hit snags, check the plugin’s built-in documentation or Urban Airship’s Flutter-specific guides for common issues.
While Xamarin’s official integration is more mature, Flutter’s setup is straightforward once you follow these steps. This should give you a fair basis to compare the two frameworks for your project needs.
内容的提问来源于stack exchange,提问作者hunteke

