当应用程序处于后台模式时获取通知

Get notification when app is in background mode

我想在收到通知时调用一个函数,即使我的应用程序在后台也是如此

我在 Whosebug 上发现了很多问题/答案 但在我身上却没有。所以我会解释我所做的一切

我激活了后台模式

here

我的info.plist很好

here

在我的 didFinishLaunchingWithOption 中我有

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Badge | .Sound | .Alert, categories: nil))
    application.registerForRemoteNotifications()

我有

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("NOTIFICATION 4");
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    println("NOTIFICATION FETCH");
}

但是我在发送通知时看不到我的日志

我的通知是这样发送的

    { deviceToken: XXXXX,
    expiration: 1442225011,
    identifier: 0,
    payload: 
    { 'content-available': '1',
    aps: { badge: 0, alert: 'Hello' } } }

我不知道该怎么办...我真的无法收到此通知,什么也没发生(我收到横幅但没有调用我的功能)

非常感谢

如您在文档中所见:

The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a “silent” notification.

所以你应该在 aps 字典中插入 content-available,像这样:

{ deviceToken: XXXXX,
expiration: 1442225011,
identifier: 0,
payload: 
{ aps: { 'content-available': '1', badge: 0, alert: 'Hello' } } }