ios10, Swift 3 et Firebase les Notifications Push (FCM)
J'ai du mal à afficher mes notifications push que j'envoie à mon appareil de la FCM console de notification. Je peux voir l'appareil est à la réception de l'avis, parce que je peux voir le message que j'ai envoyer "test8"
Connected to FCM.
%@ [AnyHashable("notification"): {
body = test8;
e = 1;
},
Mais c'est pas grave si mon application est au premier plan ou d'arrière-plan je n'ai pas la notification s'affiche.
J'ai ajouté "contexte modes de téléchargements de l'Application de contenu en réponse à des notifications push" à l'info.plist. Mes certificats sont corrects et je n'ai pas de problème de la génération d'un jeton. Mon application est de recevoir les notifications mais juste de ne pas les afficher.
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//[START register_for_notifications]
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()
}
application.registerForRemoteNotifications()
//[END register_for_notifications]
FIRApp.configure()
//Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification,
object: nil)
return true
}
//[START receive_message]
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
//If you are receiving a notification message while your app is in the background,
//this callback will not be fired till the user taps on the notification launching the application.
//TODO: Handle data of notification
//Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
//Print full message.
print("%@", userInfo)
}
//[END receive_message]
//[START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
//Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
//[END refresh_token]
//[START connect_to_fcm]
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
//[END connect_to_fcm]
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}
//[START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
//[END disconnect_from_fcm]
}
//[START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
//Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(center: UNUserNotificationCenter,
willPresentNotification 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)
}
}
extension AppDelegate : FIRMessagingDelegate {
//Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) {
print("%@", remoteMessage.appData)
}
}
// [END ios_10_message_handling]
Je avoir essayé à la recherche et à la résolution de ce problème sur mon propre mais je suis d'avoir des problèmes. Toute aide ou suggestion serait grandement apprécié.
OriginalL'auteur Paul_D | 2016-09-27
Vous devez vous connecter pour publier un commentaire.
À l'intérieur de la méthode didRegisterForRemoteNotificationswithdevicetoken, ajoutez le code suivant:
Et ne pas oublier de l'activer Notifications Push à l'intérieur de Capacités.
Ajoutez cette ligne à votre Info.fichier plist: FirebaseAppDelegateProxyEnabled et définir la valeur Booléenne à N
oui Paul, c'est un peu difficile de t'aider ici parce que vous devez suivre plusieurs étapes. J'ai fait une vidéo avec un tutoriel, mais c'est en portugais et il n'est pas dans le web encore. Mais saviez-vous importer le JSON généré par Firebase dans votre application?
Gustavo j'ai suivi vos instructions et les notifications sont désormais de travail. J'ai eu à suivre des directives et puis je l'ai désinstallé l'application à partir de mon appareil, nettoyé le projet, de reconstruire et de le réinstallé sur mon téléphone. Je suis maintenant en mesure de recevoir des notifications alors que mon application est en cours d'exécution dans le premier plan et l'arrière-plan. Je vous remercie pour votre aide. Je l'apprécie vraiment beaucoup.
Quelqu'un converti ça en Swift 3 code? erreur: Ne peut invoquer à l'initialiseur de type 'UnsafePointer<CChar>" avec une liste d'arguments de type '(UnsafeRawPointer)' -il y a donc une toute nouvelle façon pour UnsafeRawPointer
OriginalL'auteur Gustavo Barbosa
Vous avez besoin de déplacer
il ne devrait pas être à l'intérieur de l'autre.
Voir la suite de s'engager dans Firebase
Merci, résolu mon problème. Cependant, vous devez mettre à jour votre code swift 3.
OriginalL'auteur Collinizer
Pour moi, c'était le manque
entitlements
fichier, donc dans leCapabilities
de votre projet, vous devez cliquez sur corriger problème. Xcode permet de configurer le fichier pour vous.OriginalL'auteur AITAALI_ABDERRAHMANE