Azure Notification Hub是否已停止服务?求有效Android/iOS示例代码
Azure Notification Hub 有效Android/iOS示例代码
Android 实现(基于最新SDK)
1. 添加依赖
在模块级build.gradle中引入依赖:
dependencies { // Azure Notification Hub SDK implementation 'com.microsoft.azure:notification-hubs-android-sdk:1.1.4' // Firebase Messaging 依赖(Android推送依赖FCM) implementation 'com.google.firebase:firebase-messaging:23.3.1' }
2. 推送服务实现
创建自定义Firebase消息服务类,处理令牌注册和消息接收:
import com.microsoft.windowsazure.messaging.NotificationHub import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage class PushMessagingService : FirebaseMessagingService() { // 替换为你的Notification Hub信息 private val HUB_NAME = "你的Notification Hub名称" private val LISTEN_CONNECTION_STRING = "你的Listen权限连接字符串" override fun onNewToken(token: String) { super.onNewToken(token) // 将FCM令牌注册到Notification Hub NotificationHub(HUB_NAME, LISTEN_CONNECTION_STRING, this).register(token) } override fun onMessageReceived(message: RemoteMessage) { super.onMessageReceived(message) // 处理收到的推送消息 val pushTitle = message.notification?.title ?: "新通知" val pushContent = message.notification?.body ?: "你有一条新消息" // 这里可以添加本地通知弹出逻辑 } }
3. 配置Manifest
在AndroidManifest.xml中声明服务:
<service android:name=".PushMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
iOS 实现(基于最新SDK)
1. 安装依赖
通过CocoaPods添加依赖,Podfile中添加:
pod 'AzureNotificationHubs-iOS', '~> 2.0.0'
执行pod install完成安装。
2. 推送配置与注册
在AppDelegate.swift中实现推送注册逻辑:
import UIKit import UserNotifications import AzureNotificationHubs @main class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { // 替换为你的Notification Hub信息 private let HUB_NAME = "你的Notification Hub名称" private let LISTEN_CONNECTION_STRING = "你的Listen权限连接字符串" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 请求推送权限 UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in } application.registerForRemoteNotifications() return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // 注册APNs令牌到Notification Hub let hub = SBNotificationHub(connectionString: LISTEN_CONNECTION_STRING, notificationHubPath: HUB_NAME) hub.registerDeviceToken(deviceToken) { error in if let error = error { print("Notification Hub注册失败: \(error.localizedDescription)") } else { print("Notification Hub注册成功") } } } func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { // 前台接收推送时展示通知 completionHandler([.alert, .badge, .sound]) } }
注意事项
- 确保在Azure Portal中正确配置了Android的FCM密钥和iOS的APNs证书
- 连接字符串使用Listen权限的版本,避免泄露高权限密钥
内容的提问来源于stack exchange,提问作者sy-huss
相关产品推荐
相关产品推荐

