2016-01-02 57 views
0

這是我的ReceiveRemoteNotification代碼。UILocalNotification未顯示

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

+1

你應該從你的pastebin代碼中刪除你的解析applicationId和clientKey。或刪除鏈接,並創建一個新的沒有說的信息 – tskulbru

回答

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)。

如果您希望在應用程序處於活動狀態時收到遠程通知時顯示本地通知,則需要使用上述方法創建該通知。如果您想在應用程序處於活動狀態時顯示alertview,則還需要處理該方法中的兩種情況。我的建議是切換到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":""} 

你有複數形式,而不是單一的形式。看看我給出的允許密鑰的網址。 links密鑰在aps字典中不存在,這意味着它是一個自定義密鑰,需要放在字典之外。

請注意,這是標準推送通知方式,使用Parse時可能不正確。

+0

我已經更新的東西@tskulbru –

+0

是的,但你仍然需要創建一個'UILocalNotification',如果你想讓它顯示爲手機上的通知,而應用程序是活性。如果它不活動,系統會爲您處理。 – tskulbru

+0

是你對'UILocalNotification' @tskulbru的意思嗎? –