我从parse.com迁移到城市飞艇,我差不多完成了,一切正常,但有一件事。当我的应用程序处于打开状态并发送推送通知时,系统会显示警告视图不。如果我将应用程序置于背景或关闭应用程序成功 接收 通知
我尝试了不同的方法让它发挥作用,我尝试添加:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
UAirship.push().appRegisteredForRemoteNotificationsWithDeviceToken(deviceToken)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
UAirship.push().appRegisteredUserNotificationSettings()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
UAirship.push().appReceivedRemoteNotification(userInfo, applicationState: application.applicationState)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
UAirship.push().appReceivedRemoteNotification(userInfo,
applicationState:application.applicationState,
fetchCompletionHandler:completionHandler)
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
UAirship.push().appReceivedActionWithIdentifier(identifier!,
notification: userInfo,
applicationState: application.applicationState,
completionHandler: completionHandler)
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
UAirship.push().appReceivedActionWithIdentifier(identifier!,
notification: userInfo,
responseInfo: responseInfo,
applicationState: application.applicationState,
completionHandler: completionHandler)
}
将 UAConfig 更改为自动 设置 禁用:
let config: UAConfig = UAConfig.defaultConfig()
config.automaticSetupEnabled = false
UAirship.takeOff(config)
我还添加了 AppDelegate 这个:
class AppDelegate: UIResponder, UIApplicationDelegate, UAPushNotificationDelegate {
推出 此:
UAirship.push().userNotificationTypes = [.Alert, .Badge, .Sound]
UAirship.push().pushNotificationDelegate = self
但是没有 工作,我真的很沮丧,因为我无法让它工作,对我来说,Urban Airship文档,它对此事并不清楚。
如果我没有弄错的话,默认情况下应该可以正常工作,但不会这样做,但事实并非如此。
我希望有人可以帮助我,这是我完整的代码,没有做任何修改:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// Populate AirshipConfig.plist with your app's info from
// https://go.urbanairship.com
// or set runtime properties here.
let config: UAConfig = UAConfig.defaultConfig()
UAirship.takeOff(config)
UAirship.push().userPushNotificationsEnabled = true
// Clear badge
UAirship.push().resetBadge()
return true
}[...]}
我使用的是Swift 2,XCode 7.2.1和iOS8以及Urban AirShip 6.4.x
感谢
答案 0 :(得分:2)
如果您希望在收到前景通知时触发警报显示,则需要实施UAPushNotificationDelegate并在那里处理警报。在收到前景通知时显示警报不是Urban Airship SDK中的默认行为。
推送通知代理
创建一个实现UAPushNotificationDelegate的类,并包含可选的receivedForegroundNotification方法或displayNotificationAlert方法。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ??
UITableViewCell(style: .default, reuseIdentifier: identifier)
cell.textLabel!.text = "my text"
return cell
}
设置代理
class PushNotificationDelegate : NSObject, UAPushNotificationDelegate {
func receivedForegroundNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {
// Called when the app receives a foreground notification. Includes the notification dictionary.
// Call the completion handler
completionHandler(UIBackgroundFetchResult.NoData)
}
func displayNotificationAlert(alertMessage: String) {
// Called when an alert notification is received in the foreground. Includes a simple string to be displayed as an alert.
}
}
您可以查看Urban Airship的documentation作为推送通知代表。