Support » Developing with WordPress » Using Firebase to push notification from WordPress

  • saurav.rox

    (@sauravrox)


    I am trying to push a notification from a plugin using firebase so that the notification is received by the android app.
    I have the API key from google but don’t know how to get the device token so that I can push notification using CURL.
    This is a post from stackoverflow I have been following but stuck in device token. How can I get it?

    This what the link mentioned above has recommended:

    
    <?php
        $message = 'ojlaasdasdasd';
        $title = 'ojla';
        $path_to_fcm = 'https://fcm.googleapis.com/fcm/send';    $server_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5LnDZO2BpC2aoPMshfKwRbJAJfZL8C33qRxxxxxxxxxxxxL6';
        $key = 'eqnlxIQ1SWA:APA91bGf1COAZamVzT4onl66_lEdE1nWfY7rIADcnt9gtNYnw7iWTwa7AYPYTHESFholkZ89ydCQS3QeL-lCIuiWTXiqoDREO0xhNdEYboPvqg8QsBYkrQVRlrCLewC4N-hHUja1NG4f';
        $headers = array(
                        'Authorization:key=' .$server_key,
                        'Content-Type: application/json');
        
        $fields = array
                    (
                        'to'        => $key,
                        'notification'  => array('title' => $title,'body' => $message)
                    );
        
        
        $payload = json_encode($fields);
        
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, '$path_to_fcm' );
        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_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt( $ch,CURLOPT_POSTFIELDS, $payload);
        $result = curl_exec($ch);
        echo $result;
        curl_close($ch);
        ?>
    

    Any help on this would be highly appreciated.

    • This topic was modified 9 months ago by  Jan Dembowski. Reason: Fixed formatting
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Tokens are generated by the FCM SDK. There are functions you call to get the desired token. Check out the FCM docs at Google for specifics unique to your platform. If you still have trouble, I suggest you ask at one of the Google support options listed at https://firebase.google.com/support/

    This topic is not WordPress related. The chances of getting a FCM expert here is far smaller than at Firebase support. In other words, you’re asking in the wrong forum 😛 No worries though, we try to help when we can no matter what.

    saurav.rox

    (@sauravrox)

    Hello @bcworkz,

    Thank you so much for replying.

    I was very confused so I contacted one of my buddy who is an Android developer.

    I was thinking to push notification to firebase from WordPress so that android app grabs it. But it is very complicated as device token is a part of another platform and I am trying to use it in WordPress which makes no sense.

    Is there anyway you can recommend that I can connect WordPress with Android so that android app grabs and shows push notification to the app users?

    This is the scenario:

    WordPress (push notification) -> Firebase -> Android (grab notification from firebase)

    Example: show notification in android device if someone comments on any post.

    Your help is really appreciated.

    Thanks again.

    Moderator bcworkz

    (@bcworkz)

    There’s no way for WP to determine the token on its own. It must come from the app. The way I see this happening is the user authorizes notifications in their app. The app sends the necessary data to WP (via a custom REST API endpoint), including the device token. Then WP can push notifications any time it wants.

    In any case, data has to come from the app before WP can do anything. Whether it’s part of the apps installation, or an option selected by the user, or something else on the app side, or something WP triggers in its normal communication with the app, only the app can tell WP what the token is.

    saurav.rox

    (@sauravrox)

    Thanks for explaining. By your explanation it seems I am going in the opposite direction.
    As you said There’s no way for WP to determine the token on its own, by this do you mean I can connect Android to WordPress but not from WordPress to Android?
    Also, can you please make me clear how can I manage that scenario? How can I start?
    My requirement is to show the notification to App users if any action is taken in WordPress site. Is it even possible?
    Thank you again.

    Moderator bcworkz

    (@bcworkz)

    The app, in some manner, needs to first initiate some kind of communication with WP. How else can WP know the app is out there on someone’s device? It could be on installation as part of registering a new user, or after some user initiated event, like tapping “I want notifications”. What ever will do the job. The app determines the device token by whatever means (I’m not too familiar with that part) and sends it to WP at that time. Then WP can save it in user meta for use when pushing notifications.

    As far as WP is concerned, Android apps are just another client like common browsers. With few exceptions like pushes, the client always initiates any communication, whether by HTTP request, Ajax (still just HTTP), something, anything. It goes first.

    WP can send pushes through any action or filter hook that WP fires when things happen. It’s just a matter of identifying the right hook for the event.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using Firebase to push notification from WordPress’ is closed to new replies.