我正在关注microssoft's step-by-step to use AppCenterPush,但是当尝试插入“ MSPushDelegate.setDelegate(自己)”时,Xcode向我返回以下错误“类型'MSPushDelegate'没有成员'setDelegate'”。 我认为我无法注册设备或接收通知,因为我无法关联MSPushDelegate。
我当前的代码:
import UIKit
import UserNotifications
import AppCenter
import AppCenterAnalytics
import AppCenterCrashes
import AppCenterPush
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MSPushDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Clean notifications
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
//enable notification (used for local notifications)
let notificationSettings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
UIApplication.shared.registerForRemoteNotifications()
// Notification by AppCenterPush
MSPush.setDelegate(self) // this line show the error: "Type 'MSPush' has no member 'setDelegate'"
MSAppCenter.setEnabled(true)
MSAppCenter.start("{MyAppSecret}", withServices: [
MSAnalytics.self,
MSCrashes.self,
MSPush.self
])
return true
}
显示通知的代码:
func push(_ push: MSPush!, didReceive pushNotification: MSPushNotification!) {
print("Working")
let title: String = pushNotification.title ?? ""
var message: String = pushNotification.message ?? ""
var customData: String = ""
for item in pushNotification.customData {
customData = ((customData.isEmpty) ? "" : "\(customData), ") + "\(item.key): \(item.value)"
}
if (UIApplication.shared.applicationState == .background) {
NSLog("Notification received in background, title: \"\(title)\", message: \"\(message)\", custom data: \"\(customData)\"");
} else {
message = message + ((customData.isEmpty) ? "" : "\n\(customData)")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel))
// Show the alert controller.
self.window?.rootViewController?.present(alertController, animated: true)
}
}