2017-08-07 2 views
0

J'ai envoyé des notifications push en utilisant le code ci-dessous, cependant - cinq, trois, ou une notifications push sont envoyées (sur le même appareil) quand seulement on devrait être envoyé (tout le temps). C'est ce que mon PHP ressemble - il est tout droit sorti de l'exemple onesignal pour l'utilisation de PHP pour envoyer une notification push:La notification push OneSignal arrive 5 fois quand elle n'est appelée qu'une seule fois - PHP/iOS

<? 
    $contentpost = ""; 
    $appid = ""; 
    $playerIds = ""; 

    //error_log(print_r($_POST['app_id'], true)." ()*&*()*&()*&)(*&(*&)(*&()&*()*&()"); 

    foreach ($_POST as $key => $value) { 
     if($key == 'app_id') { 
      $appid = $value; 
     } 
     if($key == 'content') { 
      $contentpost = $value; 
     } 
     if($key == 'include_player_ids') { 
      $playerIds = $value; 
     } 
     error_log($key."   ".$value); 
    } 

    function sendMessage($contentr, $appidd, $players){ 
     $content = array(
      "en" => $contentr 
      ); 

     $fields = array(
      'app_id' => $appidd, 
      'include_player_ids' => array($players), 
      'contents' => $content 
     ); 

     $fields = json_encode($fields); 
     //print("\nJSON sent:\n"); 
     //print($fields); 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications"); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 
                'Authorization: Basic xxxxxx')); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
     curl_setopt($ch, CURLOPT_HEADER, FALSE); 
     curl_setopt($ch, CURLOPT_POST, TRUE); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

     $response = curl_exec($ch); 
     curl_close($ch); 

     return $response; 
    } 

    $response = sendMessage($contentpost, $appid, $playerIds); 
    $return["allresponses"] = $response; 
    $return = json_encode($return); 

    //print("\n\nJSON received:\n"); 
    print($return); 
?> 

Lorsque le code ci-dessus exécute, je reçois soit cinq, trois, ou une des notifications push envoyé à mon téléphone. J'ai besoin de toujours en être un. J'ai supprimé la fonction et j'en ai fait un script continu, ce qui n'a pas aidé. En outre, je mets:

error_log("running more than once");

au bas du script ... et il vérifie que tout le script est en cours d'exécution plus d'une fois par impression:

[07-Aug-2017 04:15:11 Europe/Berlin] running more than once 
[07-Aug-2017 04:15:11 Europe/Berlin] "{\"id\":\"f7602a22-770c-491c-84c4-ba8cb89c773a\",\"recipients\":1}" 
[07-Aug-2017 04:15:11 Europe/Berlin] running more than once 
[07-Aug-2017 04:15:11 Europe/Berlin] "{\"id\":\"c3b60d5d-0045-422c-8fea-f3dbfc312480\",\"recipients\":1}" 
[07-Aug-2017 04:15:11 Europe/Berlin] running more than once 
[07-Aug-2017 04:15:11 Europe/Berlin] "{\"id\":\"802a1e0c-33ba-48b2-95f8-36b07d3404e5\",\"recipients\":1}" 
[07-Aug-2017 04:15:11 Europe/Berlin] running more than once 
[07-Aug-2017 04:15:11 Europe/Berlin] "{\"id\":\"928d3a29-29d0-4c78-a740-bc4559018c25\",\"recipients\":1}" 
[07-Aug-2017 04:15:11 Europe/Berlin] running more than once 
[07-Aug-2017 04:15:11 Europe/Berlin] "{\"id\":\"964e1dbc-41b4-40be-8edc-548c3fe74c5e\",\"recipients\":1}" 

Vous pouvez également voir que le script crée plusieurs notifications push.

Répondre

0

J'avais besoin d'avoir mon code comme ceci, avec le bloc _managerAFNetworking en dehors du bloc de requête de la base de données Firebase.

   __block NSString *keyy = [[NSString alloc] init]; 
      [[[[self.ref child:@"userData"] queryOrderedByChild:@"username"] 
       queryEqualToValue:newtitle] 
       observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { 

       if (snapshot.value != [NSNull null]){ 
        for (NSDictionary *snap in [snapshot.value allValues]) { 
         NSLog(@"---> %@",snap); 
         NSLog(@"---> %@",snapshot.key); 
        } 

        keyy = [[NSString alloc] initWithString:snapshot.key]; 

       } 
      }]; 

      _manager = [AFHTTPSessionManager manager]; 
      _manager.responseSerializer=[AFHTTPResponseSerializer serializer]; 
      _manager.responseSerializer.acceptableContentTypes = [_manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"]; 

      NSDictionary *params = @{@"app_id": @"785c1a2c-a150-4986-a9b3-82cfe257db48", @"include_player_ids": [userdefaults objectForKey:@"uuid"], @"content": [userdefaults objectForKey:@"pfuser"]}; 

      [_manager POST:@"http://192.168.1.131:8888/send.php" parameters:params progress:^(NSProgress * _Nonnull uploadProgress) { 
       NSLog(@"%@", uploadProgress); 

      } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
       NSLog(@"%@ response object ***(*(*(*((**(", responseObject); 

      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
       NSLog(@"%@", [error description]); 
      }];