简体   繁体   中英

OneSignal push notification happens 5 times when only called once - PHP / iOS

I have been successfully sending push notifications using the below code, however - five, three, or one push notifications are being sent (to the same device) when only one should be sent (all the time). This is what my PHP looks like it - it is straight out of the onesignal example for using PHP to send a push notification:

<?
    $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);
?>

When the above code executes, I either get five, three, or one push notifications sent to my phone. I need it to always be one.

UPDATE I took away the function and just made it one continuous script and that didn't help. Also, I put:

error_log("running more than once");

at the bottom of the script...and it verifies that the entire script is running more than once by printing:

[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}"

You can also see that the script is creating multiple push notifications.

I needed to have my code like this, with the _manager AFNetworking block outside of the firebase database query block.

             __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]);
            }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM