2015-12-04 150 views
0

標題解釋發生了什麼。應用程序正在運行但已最小化。通知會顯示給用戶。當用戶點擊通知時,應用程序被帶到前臺並且方法didReceiveRemoteNotification被輸入。但是,如果用戶單擊應用程序圖標而不是恢復應用程序,則方法didReceiveRemoteNotification不會執行。didReceiveRemoteNotification沒有被調用,除非申請從通知中恢復

這裏是我的代碼:

在didFinishLaunchingWithOptions:

//FOR ALLOWING NOTIFICATIONS 
      let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
      UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
      UIApplication.sharedApplication().registerForRemoteNotifications() 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     let deviceTokenStr = convertDeviceTokenToString(deviceToken) 
     GlobalVariables.UserDefaults.setValue(deviceTokenStr,forKey: "Push_Notification_Reg_ID"); 
     RZLog.VIP("The PushNotification Device Token is: \(deviceTokenStr)") 

    } 

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 

     RZLog.Error(error.description) 
    } 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     RZLog.Debug("A NEW NOTIFICATION HAS BEEN RECEIVED") 
     //handle 

       completionHandler(UIBackgroundFetchResult.NoData) 
     } 
    } 
+0

什麼是你通知的有效載荷?它有'內容可用:'? – Paulw11

+0

我剛剛添加「內容可用」:「1」,大聲笑,它的工作。但現在它被調用兩次,以防我按下通知 –

回答

0

在後臺didReceiveRemoteNotification不會被調用。在推送通知旗幟&應用涉及到前景和didReceiveRemoteNotification被稱爲

  1. 點擊:你應該使用

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
    
        if(application.applicationState == UIApplicationStateInactive) { 
    
        NSLog(@"Inactive"); 
    
        //Show the view with the content of the push 
    
        completionHandler(UIBackgroundFetchResultNewData); 
    
    } else if (application.applicationState == UIApplicationStateBackground) { 
    
        NSLog(@"Background"); 
    
        //Refresh the local model 
    
        completionHandler(UIBackgroundFetchResultNewData); 
    
    } else { 
    
        NSLog(@"Active"); 
    
        //Show an in-app banner 
    
        completionHandler(UIBackgroundFetchResultNewData); 
    
    } 
    } 
    

    基本上didReceiveRemoteNotification只調用在下列情況下。

  2. 當App在前臺運行時,推送通知到達& didReceiveRemoteNotification被調用。

從蘋果文檔: 如果在推送通知到達時,應用程序沒有運行,該方法啓動應用程序,並提供了在啓動選項字典中的相應信息。該應用程序不會調用此方法來處理推送通知。相反,您的應用程序的實現:willFinishLaunchingWithOptions:或應用程序:didFinishLaunchingWithOptions:方法需要獲取推送通知有效載荷數據並進行適當的響應。

1

這是設計。當應用程序收到遠程通知(在後臺或前臺)時以及用戶從通知中啓動應用程序時調用此方法。

如果用戶點擊應用程序圖標,只有

applicationWillEnterForeground: 

applicationDidBecomeActive: 

將被稱爲