Android:将Notifcation推向指定的应用程序包 [英] Android: Push Notifcation toward an specified application package

查看:56
本文介绍了Android:将Notifcation推向指定的应用程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置服务器以发送推送通知.

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

我正在尝试向指定软件包的用户发送推送通知,例如类似于com.mysite.android(Firebase控制台中提供此选项). 检查之类的答案,我无法理解如何设置to参数以将推送通知发送给指定软件包的用户.我可以找到将通知发送到specific devices by their idsnews topics的示例.

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;
        }

        ?>

注意:我故意删除了server_key.

推荐答案

这是可管理的.但是,要使您能够将通知发送到应用程序,首先需要成为有效的发件人.

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

通常,(如果您是应用程序所有者)要做的就是将应用程序添加到Firebase项目中,生成google-services.json文件并将其添加到您的Android项目中.

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.

如果您不是应用程序所有者,并且不想通过Firebase项目添加应用程序,则可以通过简单地修改应用程序中使用的google-services.json并在其中添加SenderID来覆盖它.但是我非常怀疑您是否有权修改一开始不是您自己的应用.

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.

注意:建议您不要修改google-services.json.

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.

然后在发送主题时,只需在to参数中使用包的名称,如下所示:

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>"

这篇关于Android:将Notifcation推向指定的应用程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆