Push, via php, for all users

Asked

Viewed 991 times

0

I am working on a project with Ionic 3 and backend in php/mysql. At the current stage I need to send push notifications and chose to work with firebase and phonegap-plugin-push. I installed and tested via firebase console and the notification was sent correctly. Having done that, I came up with a php template for sending notifications without using the firebase console, but I found a difficulty: I don’t know how to send a notification to all devices (and not by id).

Follow the PHP code used:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// set only for one for safety
$registrationId = $_GET['id'];
$texto = $_GET['texto'];
// prep the bundle
$msg = array
(
'title' => 'Notificação',
'body' => $texto,
'icon' => 'myicon',
'sound' => 'default'
);
$fields = array
(
'priority' => 'high',
'to' => $registrationId,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

Note. 1: The firebase console has both options.

Note. 2: The above code works correctly when informed the id.

What to put in the 'to' => $registrationId, so that all devices receive notification?

  • Why not inform the ID? and it is mandatory

  • Have you tried using the Onesignal platform? I use it and it’s really good. (https://onesignal.com/) If you need help using it, it says there that I can help you.

  • What would the code look like to use the oneSignal platform?

1 answer

-2

No no 'to' => $registrationId you must enter your app token on your Android project add these lines into your mainactivity inside the oncreate () {}

//this code will generate the token in the Logcat of Android Studio, put the name Tokenapp in the search and you will see a huge code with letters and numbers

            FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull Task<String> task) {
                    String token = task.getResult();
                    Log.d("TokenApp", token);
                }
            });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.