2016-05-31 32 views
5

Sto integrando la notifica FCM e il servizio cloud nella mia app. Ho seguito esattamente le stesse fasi menzionate nel documento firebase. Anche io ho provato con il codice di esempio dato da FCM. è solo lanciando alcuni avvertimenti come <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "The operation couldn’t be completed. and <FIRMessaging/WARNING> FIRMessaging registration is not ready with auth credentials.La notifica FCM non funziona in ios

Qualcuno può aiutarmi a risolverlo. sto googling da una settimana riguardo ma niente di utile ho avuto. Il mio codice scritto in Appdelegate.m è

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Register for remote notifications 
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 
     // iOS 7.1 or earlier 
     UIRemoteNotificationType allNotificationTypes = 
     (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge); 
     [application registerForRemoteNotificationTypes:allNotificationTypes]; 
    } else { 
     // iOS 8 or later 
     // [END_EXCLUDE] 
     UIUserNotificationType allNotificationTypes = 
     (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
     UIUserNotificationSettings *settings = 
     [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 

    // [START configure_firebase] 
    [FIRApp configure]; 
    // [END configure_firebase] 

    // Add observer for InstanceID token refresh callback. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
               name:kFIRInstanceIDTokenRefreshNotification object:nil]; 
    return YES; 
} 

// [START receive_message] 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
    // 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. 
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); 

    // Pring full message. 
    NSLog(@"%@", userInfo); 
} 
// [END receive_message] 

// [START refresh_token] 
- (void)tokenRefreshNotification:(NSNotification *)notification { 
    // Note that this callback will be fired everytime a new token is generated, including the first 
    // time. So if you need to retrieve the token as soon as it is available this is where that 
    // should be done. 
    NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 
    NSLog(@"InstanceID token: %@", refreshedToken); 

    // Connect to FCM since connection may have failed when attempted before having a token. 
    [self connectToFcm]; 

    // TODO: If necessary send token to appliation server. 
} 
// [END refresh_token] 

// [START connect_to_fcm] 
- (void)connectToFcm { 
    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"Unable to connect to FCM. %@", error); 
     } else { 
      NSLog(@"Connected to FCM."); 
     } 
    }]; 
} 
// [END connect_to_fcm] 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    [self connectToFcm]; 
} 

// [START disconnect_from_fcm] 
- (void)applicationDidEnterBackground:(UIApplication *)application { 
    [[FIRMessaging messaging] disconnect]; 
    NSLog(@"Disconnected from FCM"); 
} 
// [END disconnect_from_fcm] 

Sperare di ottenere una risposta al più presto. Grazie in anticipo!

+0

Come si invia il messaggio? Stai utilizzando la console di Firebase e il targeting di tutti i dispositivi? Hai aggiunto GoogleService-Info.plist? Puoi anche provare a implementare l'applicazione: didFailToRegisterForRemoteNotificationsWithError per ulteriori informazioni – Chris

+0

Sto inviando messaggi tramite la console di Firebase (notifica) e ho aggiunto googleService-Info.plist. didFailtoRegister non viene chiamato. Ricevo il token dispositivo ma nessuna notifica sul dispositivo – ios

+0

e yaa, ho come target un singolo dispositivo e inoltre inserisco il token di registrazione FCM nel campo obbligatorio. Nella console di Firebase, lo stato del messaggio è completato ma nessuna notifica push sul dispositivo. ho controllato con entrambi gli stati, ovvero avere app in background e in primo piano. – ios

risposta

0

Stavo facendo la stessa cosa con la mia app per iOS. FCM e GCM NON FUNZIONANO con iOS. Non importa quello che qualcuno dice che non c'è alcuna possibilità che funzioni. Trascorro 2 settimane e sono andato ovunque sulla rete chiedendo e guardando un post. iOS accetterà solo notifiche push APNS. Salva te stesso il mal di testa e inizia con le app.

MODIFICA: Ecco il repository più recente per FCM. Dare un colpo e vedere cosa ne viene fuori. https://github.com/firebase/quickstart-ios Sembra un po 'diverso da quello che ho provato quando stavo lavorando sulla mia versione iOS. @Chris, è questo il ramo del codice che hai usato per implementare la tua versione?

+0

FCM delegati ad APNS se l'app è inattiva. Ho inviato correttamente una notifica push dalla console di Firebase al mio iphone (il simulatore non supporta le notifiche push) – Chris

+0

Stai utilizzando il numero APNS o stai utilizzando il token dispositivo GCM/FCM – MNM

+0

Perché se usi solo GCM, per favore esegui un tutorial completo con procedura passo passo per eseguire ciò perché mi piacerebbe sapere come farlo – MNM

0

Ho anche lo stesso tipo di problema. Ricevo il messaggio e il token "Connesso a FCM" e sembra tutto perfettamente integrato, ma non in grado di ricevere alcuna notifica.

Tuttavia, ho ricevuto una notifica in una sola volta, ma successivamente senza modifiche al codice e modifiche o rimozione del certificato, non sono in grado di ricevere ulteriori notifiche.

Controlla qui il mio codice, è un'app cocos2d.

func didLoadFromCCB() 
    { 
    ... 
    setupPushNotification() 
} 

func setupPushNotification() { 
     let application = UIApplication.sharedApplication() 

     // Register for remote notifications 
     if #available(iOS 8.0, *) { 
      let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } else { 
      // Fallback 
      let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound] 
      application.registerForRemoteNotificationTypes(types) 
     } 

     FIRApp.configure() 

     // Add observer for InstanceID token refresh callback. 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "tokenRefreshNotification:", 
      name: kFIRInstanceIDTokenRefreshNotification, object: nil) 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive:", name: UIApplicationDidBecomeActiveNotification, object: nil) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil) 
    } 


// [START refresh_token] 
    func tokenRefreshNotification(notification: NSNotification) { 
     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 didBecomeActive(application:UIApplication) { 
     NSLog("Did Become Active") 
     connectToFcm() 
    } 

    func didEnterBackground(application: UIApplication) { 
     NSLog("Did enter background") 
     FIRMessaging.messaging().disconnect() 
     NSLog("Disconnected from FCM.") 
    } 
} 

Non so come ho ottenuto che solo una notifica corretta poi ha trasformato le tabelle. :(

Fatemi sapere come risolvere questo.,

+0

Metti questo in un'altra domanda non qui – MNM

0

provare a inviare questo

curl --header "Authorization: key={YOUR_API_KEY}" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"{YOUR_DEVICE_TOKEN}\",\"content_available\":true,\"priority\":\"high\",\"notification\":{\"title\":\"TEST\",\"sound\":\"default\",\"body\":\"IT WORKS!\",\"badge\":1}}" 

funziona per me (ovviamente, registrarsi per le notifiche push prima il. n stampare il token del dispositivo)

Problemi correlati