【问题标题】:UILocalNotification didn't showed upUILocalNotification 没有出现
【发布时间】:2016-01-02 11:44:03
【问题描述】:

这是我的 ReceiveRemoteNotification 代码。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFPush.handlePush(userInfo)
        if application.applicationState == UIApplicationState.Inactive {
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        }

        let notifiAlert = UIAlertView()

        let aps = userInfo["aps"] as? [NSObject: AnyObject]
            let alert1 = aps!["alerts"] as? String
            let link1 = aps!["links"] as? String

            notifiAlert.title = alert1!
            notifiAlert.message = link1
            notifiAlert.addButtonWithTitle("OK")
            notifiAlert.show()

            print(userInfo)
            print("success")

    }

我尝试从我的网络接收 JSON 数据,我的 JSON 看起来像:

{"aps":{"alerts":"3","links":"","sounds":"default"}}

我确实收到了 Json 数据并将数据转换为视图。使用此代码:

let aps = userInfo["aps"] as? [NSObject: AnyObject]
                let alert1 = aps!["alerts"] as? String
                let link1 = aps!["links"] as? String

                notifiAlert.title = alert1!
                notifiAlert.message = link1
                notifiAlert.addButtonWithTitle("OK")
                notifiAlert.show()

                print(userInfo)
                print("success")

        }

但是,我只在使用应用程序时收到,但如果我不使用,我将无法收到通知。但我的手机振动或发出声音,但没有消息。

我在这里错过了什么吗?谢谢!

已添加这是我的完整代码http://pastebin.com/mkFiq6Cs

编辑

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
             if application.applicationState == UIApplicationState.Inactive {
                 PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
             }
 let aps = userInfo["aps"] as? [NSObject: AnyObject]
                    let alert1 = aps!["alerts"] as? String
                    let link1 = aps!["links"] as? String

                    notifiAlert.title = alert1!
                    notifiAlert.message = link1
                    notifiAlert.addButtonWithTitle("OK")
                    notifiAlert.show()

                    print(userInfo)
                    print("success")
}

所以我必须添加这段代码:

  func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    if let userInfo = notification.userInfo {
        let aps = userInfo["aps"] as? [NSObject: AnyObject]
        let alert2 = aps!["alerts"] as? String
        let link2 = aps!["links"] as? String
        print("didReceiveLocalNotification: \(aps)")
        print("didReceiveLocalNotification: \(alert2)")
        print("didReceiveLocalNotification: \(link2)")
    }
}

这在 didFinishLaunchingOption :

 if let options = launchOptions {
    if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
        if let userInfo = notification.userInfo {
            let customField1 = userInfo["CustomField1"] as! String
            // do something neat here
        }
    }
}

已解决

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
        let notifiAlert = UIAlertView()
        if let aps = userInfo["aps"] as? [NSObject: AnyObject],
            let alert1 = aps["alert"] as? String,
            let content1 = aps["content-available"] as? String,
            let link1 = userInfo["links"] as? String {
                    notifiAlert.title = alert1
                    notifiAlert.message = link1
                    notifiAlert.addButtonWithTitle("OK")
                    notifiAlert.show()
            }

        print(userInfo)
        print("success")

    completionHandler(UIBackgroundFetchResult.NewData)
}

这非常有效。 !感谢@tskulbru

【问题讨论】:

  • 您可能应该从您的 pastebin 代码中删除您的解析 applicationId 和 clientKey。或者删除链接并创建一个没有上述信息的新链接

标签: ios json swift swift2


【解决方案1】:

这是因为application(_:didReceiveRemoteNotification:) 仅在应用程序处于活动状态时被调用。它无法处理应用非活动/后台远程通知。

如果远程通知到达时应用程序未运行,则该方法会启动应用程序并在启动选项字典中提供适当的信息。应用程序不会调用此方法来处理该远程通知。相反,您对 application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions: 方法的实现需要获取远程通知负载数据并做出适当的响应。

来源:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification

如上所述,application:didReceiveRemoteNotification 需要额外实现才能处理后台通知。

如果您只想要一种方法来为您处理所有好东西,并且没有一堆不同的方法来实现基本相同的东西,Apple 提供了一个名为 application:didReceiveRemoteNotification:fetchCompletionHandler: 的方法来为您处理所有这些.

您需要实现application:didReceiveRemoteNotification:fetchCompletionHandler: 方法而不是这个方法来处理后台通知。这会处理后台任务,然后您可以在完成时使用适当的枚举调用 completionHandler (.NoData / .NewData / .Failed)。

如果您希望在应用程序处于活动状态时收到远程通知时显示本地通知,您需要在上述方法中创建它。如果您想在应用程序处于活动状态时显示警报视图,您还需要处理该方法中的两种情况。我的建议是改用application:didReceiveRemoteNotification:fetchCompletionHandler: 并在那里实现所有内容。

所以在你的情况下,我会做这样的事情:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
  PFPush.handlePush(userInfo)
  if application.applicationState == UIApplicationState.Inactive {
    PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
  } else { 
    let notifiAlert = UIAlertView()
    if let aps = userInfo["aps"] as? [NSObject: AnyObject],
      let alert1 = aps["alerts"] as? String,
      let link1 = aps["links"] as? String {

      notifiAlert.title = alert1
      notifiAlert.message = link1
      notifiAlert.addButtonWithTitle("OK")
      notifiAlert.show()
    }

    print(userInfo)
    print("success")
  }
  completionHandler(UIBackgroundFetchResult.NewData)
}

我没有测试过代码,但它应该是类似的,没有太多的变化。我没有使用 Parse,所以我不知道 PFPush.handlePush(userInfo) 到底是做什么的,你需要查看文档。

看起来您的 apns 有效负载也有问题。见https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

在你的情况下:

{"aps":{"alert":"alert content","sound":"default", "content-available": 1}, "links":""}

你有复数形式而不是单一形式。查看我为允许的键提供的 url。 links 键在 aps 字典中不存在,这意味着它是一个自定义键,需要放在字典之外。

请注意,这是标准的推送通知方式,在使用 Parse 时可能不正确。

【讨论】:

  • 是的,但是如果您想在应用程序处于活动状态时让它在手机上显示为通知,您仍然需要创建一个UILocalNotification。如果它处于非活动状态,系统将为您处理。
  • 这就是UILocalNotification@tskulbru 的意思吗??
  • 是的,打开应用程序时成功了。警报出现了。但是,当我在主屏幕上,而应用程序仍在后台运行时,它只会发出振动或声音。当我关闭我的屏幕它什么都没有。甚至没有声音。 @tskulbru 或者我的 JSON 错误?
  • 啊,是的,你的有效载荷也是错误的。见developer.apple.com/library/ios/documentation/…
  • 我的 JSON 遗漏了什么?徽章 ?在这种情况下我应该使用什么有效载荷?我试图匹配我的 JSON alrdy @tskulbru
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多