universal-tools / UTNotificationsFeedback

7 stars 0 forks source link

Problems with registration ID #85

Open Frantrax82 opened 6 years ago

Frantrax82 commented 6 years ago

Hi everybody! these days I have been some troubles during the ocnfiguration of the push notificactions. The local notifications works perfect but I have priblems to send notifications via server, when I send a notification, I received this response:

{"multicast_id":9173181968242791511,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

In he unity side, I have write this code:

UTNotifications.Manager.Instance.OnSendRegistrationId += SendRegistrationId;
UTNotifications.Manager.Instance.OnNotificationClicked += OnNotificationClicked;
UTNotifications.Manager.Instance.OnNotificationsReceived += OnNotificationsReceived;
bool initialized = Manager.Instance.Initialize(true, 0, false);

I save a registration ID in my own database, but when I try yo send a message, the InvalidRegistration issue apears.

Here is the server side code, that sends the push notifications:

`//FCM API end-point $url = 'https://fcm.googleapis.com/fcm/send'; //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key $server_key = API_ACCESS_KEY;

    $fields = array();        
    $fields['data'] = $data;
    //$fields['to'] = '/topics/alerts';
    if (is_array($target)) {
        $fields['registration_ids'] = $target;
    } else {
        $fields['to'] = $target;
    }
    //header with content_type api key
    $headers = array(
        'Content-Type:application/json',
        'Authorization:key=' . $server_key
    );
    //CURL request to route notification to FCM connection server (provided by Google)          
    $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_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Oops! FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
    return $result;`

Thanks!!!!!

yuriy-universal-ivanov commented 6 years ago

Hi @Frantrax82 ,

First of all, thank you for using UTNotifications! Have you tested it first with DemoServer provided, does it have the same issue? If yes, please check carefully if the android package id used in Unity Android platform settings and the one used when registered the app in Firebase Console match. Please also make sure you've specified google-services.json in UTNotifications settings in Unity. But if it works alright with DemoServer, try comparing what messages it and your server send to FCM servers, what's different between them. Unfortunately without specific values of the variables in the server code you've posted, I can't tell whether the messages you're sending to FCM servers are correct or not.

Please let me know if any of the ideas above worked or not. In the later case, please write us to universal.tools.contact@gmail.com: it might require getting your devices registration ids or even server keys (which you then will change of course) so I could take a better look into that issue.

Best regards, Yuriy, Universal Tools team.