2015-07-13 12 views
5

Sto lavorando per far funzionare le notifiche in background su IOS con GCM - le notifiche non in background funzionano già. Qui sono i miei passi per integrare le notifiche di fondo:IOS GCM notifiche push non ricevute in background usando PHP sender e Swift

  1. Attiva il tag-remoti notifiche in UIBackgroundmodes
  2. aggiungere la chiave di contenuto disponibile al mio carico utile di notifica.
  3. Scrivi l'applicazione: didRecieveRemoteNotification: fetchCompletionHandler: in my delegate.

Ecco il codice per la funzione delegato:

func application(application: UIApplication, 
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) { 
     println("Notification received2: \(userInfo)") 

     GCMService.sharedInstance().appDidReceiveMessage(userInfo); 

     NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil, 
      userInfo: userInfo) 
     handler(UIBackgroundFetchResult.NoData);  
} 

Questo è il codice per lo script php on-server:

<?php 
$regId = $_GET["regId"]; 
$message = $_GET["message"]; 

$data = array('price' => $message, 'sound' => 'default', 'body' => 'helloworld', 'title' => 'default', 'badge' => 12, 'content-available' => 1); 

$ids = array($regId); 

sendGoogleCloudMessage( $data, $ids); 

function sendGoogleCloudMessage($data, $ids) 
{ 

    $apiKey = THE-API-KEY-THAT-I-AM-USING; 

    $url = 'https://android.googleapis.com/gcm/send'; 

    $post = array(
       'registration_ids' => $ids, 
       'data'    => $data, 
       'content-available' => 1, 
    ); 

    $headers = array(
        'Authorization: key=' . $apiKey, 
        'Content-Type: application/json' 
       ); 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_POST, true); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); 

    $result = curl_exec($ch); 

    if (curl_errno($ch)) 
    { 
     echo 'GCM error: ' . curl_error($ch); 
    } 

    curl_close($ch); 

    echo $result; 
} 
?> 

ho cercato di inviare il flag contenuti disponibili attraverso sia l'array interno "data" che l'array "post" esterno, che ho indicato aggiungendolo ad entrambi.

Il messaggio non viene ricevuto dalla funzione fetchCompletionHandler, ma attende fino a quando l'app non è di nuovo attiva e viene ricevuta dalla normale applicazione: didRecieveRemoteNotification. Quale potrebbe essere il motivo per cui la mia notifica non viene ricevuta tramite la funzionalità in background?

risposta

2

è necessario fornire content_available a GCM, non contenuti disponibili (come in APN) e si sarà in grado di ricevere le notifiche push in background.

https://developers.google.com/cloud-messaging/server-ref#send-downstream

P.S. - Meno di cinque minuti fa ho avuto assolutamente lo stesso problema. Dovremmo leggere più attentamente i documenti ...

+0

Grazie mille! Questo è stato sicuramente un problema - tuttavia, quando aggiungo il tag corretto, ora nessuna delle funzioni del ricevitore chiama. Qualche idea del perché? –

+1

@JohnSzymanski dovresti anche aggiungere un tag 'fetch' a UIBackgroundModes. –

+0

Sembra non cambiare nulla. . . –

Problemi correlati