2016-10-07 37 views
1

iOS 10で通知を受け取るにはどうすればよいですか?以前のバージョンでは、私はfunc application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])で通知を受け取ることができます。プッシュ通知がiOS 10に表示されない

iOS 10では、AppleがUNNotificationSettingsを紹介しています。私はfunc applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage)でFCMを得ることができます。

しかし、私の電話機にはバックグラウンドで通知が表示されません。

enter image description here

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    FIRApp.configure() 

    if #available(iOS 10.0, *) { 
     let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
      authOptions, 
      completionHandler: {_,_ 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) 
     application.registerForRemoteNotifications() 
    } 


    return true 
} 

@available(iOS 10, *) 
extension AppDelegate: UNUserNotificationCenterDelegate, FIRMessagingDelegate { 
// Receive displayed notifications for iOS 10 devices. 
func userNotificationCenter(center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    let userInfo = notification.request.content.userInfo 
    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print("%@", userInfo) 

} 


// Receive data message on iOS 10 devices. 
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) { 
    print("%@", remoteMessage.appData) 
} 
} 
+0

私は、これは全くのに役立ちますかどうかわからないんだけど、iOSの10にいくつかの新しい要件が、私はcompabilities画面を確認していたもので、それは私が追加する必要がありましたプッシュ通知の下で私のために警告を示すことを追加されました「APS環境=開発」プロパティーを持つエンタイトルメント・ファイル。エラーや警告が出ますか? – Scriptable

+0

はい、既にあります。また、私はエラーや警告を得ることはありません。通知は表示されません。 – WeiJay

+0

はiOS10用の登録デバイスですか? – Cruz

答えて

0

あなたは上記のreturn文には、この方法に

application.registerForRemoteNotifications()

を移動する必要があります。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 
-> Bool { 
    FIRApp.configure() 

    if #available(iOS 10.0, *) { 
     let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
      authOptions, 
      completionHandler: {_,_ 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)   
    } 

    application.registerForRemoteNotifications() 

    NotificationCenter.default.addObserver(self, 
              selector: #selector(self.tokenRefreshNotification), 
              name: .firInstanceIDTokenRefresh, 
              object: nil) 

    return true 
} 
関連する問題