Notifications with firebase + php

1

Hello help what I want is the following I am trying to send a notification to users of an app I know that this is done by means of token but that is my concern I do not want to send them by token I want to send it to all users they have the app installed on their device as per say per package as it is done from the firebase console .. here is my code

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_ACCESS_KEY' );
$registrationIds = [];
// prep the bundle
$msg = array
(
    'body'  => $valorDescripcion,
    'title'     => $valorTipo,
    'vibrate'   => 1,
    'sound'     => 1,
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    '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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
    
asked by Cediendo_Lo_Aprendido 05.07.2017 в 21:44
source

1 answer

0

What you can do is send messages by topic.
You create a theme and in the app you make everyone subscribe to that same topic.
Then, from the php, when you send the message you send it to the subject instead of specific devices.

This allows you to avoid the issue of registering tokens on a server and keep that list updated.

In the php the message would be:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "to": "/topics/foo-bar",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

(Instead of the tokens goes the 'to:'. foo-bar is the name of the theme in the example you put yours.)

In this link of Firebase Cloud Messaging are the instructions.

    
answered by 08.07.2017 / 19:38
source