0

在iOS10上打開應用時,我收到推送通知,但是當我按Home按鈕並且應用處於後臺模式時 - 它不會收到任何內容。爲什麼設備在後臺沒有收到推送通知?

我的代碼是:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate { 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
if #available(iOS 10.0, *) { 
     let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Sound, .Alert, .Badge], completionHandler: { (granted: Bool, error: NSError?) in 

      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.currentNotificationCenter().delegate = self 
      // For iOS 10 data message (sent via FCM) 
      FIRMessaging.messaging().remoteMessageDelegate = self 
     }) 
    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

// Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 

    application.registerForRemoteNotifications() 

} 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    let userInfo = notification.request.content.userInfo 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
    //Handle the notification 
    print("User Info => ",response.notification.request.content.userInfo) 
    completionHandler() 
} 

所以我無法找到問題出在哪裏?任何想法?或者iOS10中有新的東西?

我已經開啓這些功能:

enter image description here enter image description here

+0

FUNC申請(申請:UIApplication的,didReceiveRemoteNotification USERINFO:[NSObject的:AnyObject],fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - >空隙){ completionHandler(.NewData) } 需要背景取 – dragoneye

回答

1

您是否嘗試過這樣做

enter image description here

enter image description here

做後請確認本質上,我將編輯這個答案相應

+0

是的,我確實已經。我現在更新我的問題 –

+0

已更新 –

+0

是.pem u生成的正常工作?請使用終端檢查生產/沙箱服務器上是否有正確的SSL握手。 雖然不太可能因爲你在應用程序處於前臺時獲得推送,但它仍然很好地回溯步驟 –

0

嘗試用此步驟iOS的10,

  • 首先在目標 - >性能 - >啓用推送通知添加推送通知應享權利。

  • 第二個實施UserNotifications.framework到您的應用程序。在您的AppDelegate中導入UserNotifications.framework。

    進口< UserNotifications/UserNotifications.h>

    @interface AppDelegate中:UIResponder < UIApplicationDelegate,UNUserNotificationCenterDelegate>

    @end

  • 第三步 - 裏面didFinishLaunchingWithOptions方法分配UIUserNotificationSettings和實施UNUserNotificationCenter代表。

    定義SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(V)([[[的UIDevice currentDevice] systemVersion]比較:V選項:NSNumericSearch] = NSOrderedAscending!)

    - (BOOL)申請:(UIApplication的*)應用程序didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {

    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){ 
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
        center.delegate = self; 
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ 
         if(!error){ 
          [[UIApplication sharedApplication] registerForRemoteNotifications]; 
         } 
        }]; 
    } 
        return YES; 
    } 
    
    • 最後一步 - 實現這兩委託方法。

// ====================對於iOS 10 ============= =======

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ 

    //Called when a notification is delivered to a foreground app. 

    NSLog(@"Userinfo %@",notification.request.content.userInfo); 

    completionHandler(UNNotificationPresentationOptionAlert); 
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 

    //Called to let your app know which action was selected by the user for a given notification. 

    NSLog(@"Userinfo %@",response.notification.request.content.userInfo); 

} 

我希望它能幫助你。

謝謝。

相關問題