【问题标题】:Urban Airship not showing alert within the appUrban Airship 未在应用程序中显示警报
【发布时间】:2016-02-22 23:28:21
【问题描述】:

我正在从 parse.com 迁移到城市飞艇,我快完成了,一切正常,但只有一件事。当我的应用程序打开时,会发送推送通知,提醒视图 IS NOT 正在显示。如果我将应用置于背景关闭应用我成功收到通知强>.

我尝试了不同的方法来使其工作,我尝试添加:

    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 更改为 automatic setup disabled

        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

谢谢

【问题讨论】:

    标签: ios push uialertview urbanairship.com


    【解决方案1】:

    如果您想在收到前台通知时触发警报显示,您需要实现UAPushNotificationDelegate 并在那里处理警报。在收到前台通知时显示警报不是 Urban Airship SDK 中的默认行为。

    推送通知委托

    创建一个实现 UAPushNotificationDelegate 并包含可选的 receivedForegroundNotification 方法或 displayNotificationAlert 方法的类。

    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.
        }
    
    
    }
    

    设置委托

    UAirship.push().pushNotificationDelegate = customPushDelegate
    

    您可以查看 Urban Airship 的 documentation 以获得推送通知委托。

    【讨论】:

    • 我还缺少其他东西,因为它仍然无法正常工作:/
    • 如何配置推送通知委托?必须设置在UAirship.takeOff(config)之后。
    • 就是这样。我在 iPhone 上收到推送,但未显示警报。我必须在这些方法中创建警报吗?
    • 没错。您需要在 receivedForegroundNotification 或 displayNotificationAlert 方法中创建警报视图。
    • 如果我想添加横幅而不是警报怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多