1

I'm trying to setup a server to send push notifications.

I'm trying to send push notification toward user of an specified package e.g. something like com.mysite.android (This options is available in Firebase console). Checking answers like this I could not understand how to set to parameter to send push notifications to user of an specified package. I can find sample which send notification to specific devices by their ids or news topics.

Clarification: I'm the owner of application which I want sent push notifications.

If it helps here is my code:

<?php
    /*  
    Parameter Example
        $data = array('post_id'=>'12345','post_title'=>'A Blog post');
        $target = 'single tocken id or topic name';
        or
        $target = array('token1','token2','...'); // up to 1000 in one request
    */

        echo "Start<br/>";
        $result = sendMessage('{"id":"hello"}',null);
        var_dump($result);

        function sendMessage($data,$target){
            //FCM api URL
            $url = 'https://fcm.googleapis.com/fcm/send';
            //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
            $server_key = 'xxxxxxxxxxxxxxxxxxxkeyxxxxxxxxxxxxxxxxxxxxxxxx';

            $fields = array();
            $fields['data'] = $data;
            $fields['restricted_package_name']='com.mysite.android ';
            $fields['dry_run']=true;

            if (isset($target)){
                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
                );

            $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('FCM Send Error: ' . curl_error($ch));
            }
            curl_close($ch);
            return $result;
        }

        ?>

Notice: I removed my server_key intentionally.

Community
  • 1
  • 1
VSB
  • 9,825
  • 16
  • 72
  • 145

2 Answers2

0

You cannot send push notifications to any external app except the apps you own, simply the user device token and your server key are linked under one project so you must use a server key and valid device tokens for "X" app

user101530
  • 87
  • 12
  • I know. I want to send information to my own package. So the question is device token same for all users of same android package? I guess it is not. – VSB Oct 14 '16 at 23:02
  • no sir the device token is a unique identifier for every device so you will never find duplicated tokens in the same package – user101530 Oct 14 '16 at 23:08
  • Each device has a unique token that is generated when the user calls the Firebase Authentication method on the device. After getting the token, the device should send the token to the server and assign it to the user's account, so each user has his own unique token. That way, you can send the notification to specific users using their tokens depending on the business need. The specified package is your application, as one Firebase Project can have multiple applications inside it, each having a different package name, so you have to specify which of those applications you want to target. – MohanadMohie Oct 14 '16 at 23:46
  • On contrary, I was able to send a Notification to an app that isn't added in my Firebase Project. – AL. Oct 14 '16 at 23:52
  • @MohanadMohie While the using Firebase Auth generates token, I don't see why you have to use it. FCM already has the registration token that is also unique. – AL. Oct 15 '16 at 00:07
  • @AL. I'm not sure how you can send a Notification to an application that is not in your Firebase Project, but I've just started trying out FCM a week ago, so I'm still a little foggy about the details. As for the token, as per [this page](https://firebase.google.com/docs/notifications/android/console-device), to send a notification to a single device, you must intercept the token in `onTokenRefresh() ` and then send it to your server (`sendRegistrationToServer(refreshedToken);`) , so that you can use this token for communicating with this specific device in the future. – MohanadMohie Oct 15 '16 at 00:41
  • @MohanadMohie I added in an answer. I mentioned how it's possible. The `onTokenRefresh` is called only if the token gets invalidated. The token is generated by the InstanceID service on initial install. I'm not sure how or why you mentioned Firebase Auth here though. – AL. Oct 15 '16 at 03:57
  • I'm the owner of app. I updated my question to clarify this. – VSB Oct 15 '16 at 04:23
  • @VSB - If you're the owner of the app, then you can just add it in your Firebase Console, generate the google-services.json file and add it to your Android Project. It should work then. I also edited my answer. – AL. Oct 15 '16 at 14:45
  • When I said Firebase Authentication what I was thinking of is just the initializtaion of FirebaseApp. My apologies for any confusion I might have caused. – MohanadMohie Oct 16 '16 at 11:00
0

This is manageable. However, for you to be able to send a Notification to an app, you first need to be a valid Sender.

By that, usually (if you are the app owner), all you have to do is Add the App to your Firebase Project, generate the google-services.json file and add it to your Android Project.

If you're not the app owner and you don't want to add the app via your Firebase Project, this can be overridden by simply modifying the google-services.json that is used in the app, adding your SenderID in it. But I highly doubt that you'd be given access to modify an app that isn't yours in the first place.

Note: It is not advisable to modify your google-services.json though.

If you were able to set your peoject as a valid sender, you will have to make use of Topic Messaging and subscribe the devices to a topic name that is their app package name.

Then when sending to the topic, simply use the name of the package in the to parameter like so:

"to": "/topics/<app_package_name_here>"
AL.
  • 36,815
  • 10
  • 142
  • 281
  • @VSB Hi. Were you able to try this out? Was it any good? If so, please accept this as the correct answer in order for your post to be properly tagged. Cheers! – AL. Oct 18 '16 at 13:02
  • Hi, I tried this. This is not working. You can't send notification to all devices with the given package name using this. – Rizwan Nov 08 '17 at 07:44
  • @Rizwan This solution is using Topics with FCM. If you want to send to a specific app package *without* subscribing them first *and if* you are the owner of the project, you could just use the Firebase Notifications Console. I would appreciate it if you could put in more explanation for the downvote. – AL. Nov 08 '17 at 09:09
  • I have down voted because of this "to": "/topics/ – Rizwan Nov 08 '17 at 09:38
  • @Rizwan *sigh* That's why I said that "*..you will have to make use of **Topic Messaging** and subscribe the devices to a topic name that is their app package name.*". I didn't say that there was an existing API from FCM that does this. This is a workaround that actually works, *if* you managed to implement the Topics Messaging. – AL. Nov 08 '17 at 09:44
  • 1
    Then why package name. One can use any name. This answer was misleading the user. – Rizwan Nov 08 '17 at 10:53
  • @Rizwan If you read it carefully, it's not misleading. *sigh* This answer's been here since 2016 and only received a downvote today. If it were misleading, I would've expected *a lot* of the same comment. – AL. Nov 08 '17 at 10:56
  • For sending group notification we can create a group at server and then can send notification to these groups which you have not mentioned. Which is given in the fcm documentation https://firebase.google.com/docs/cloud-messaging/android/device-group. – Rizwan Nov 08 '17 at 10:58
  • @Rizwan Like I said. It's a workaround (the only valid one for this question). Now you're just stating the obvious. (*smh*) Good luck with whatever you're doing. – AL. Nov 08 '17 at 11:01