【问题标题】:Laravel Push Notification and MessagingLaravel 推送通知和消息传递
【发布时间】:2019-01-03 06:58:39
【问题描述】:

我正在尝试使用 laravel 编写带有后端离子的前端。 我想做的是,当用户订购应用程序时,我想向应用程序发送通知并从应用程序与用户进行消息传递。

为此,我已经查看了 /laravel-push-notification 包;

但是,它对我不起作用,因为我需要动态消息。你能帮忙吗?我不想使用https://pusher.com

【问题讨论】:

    标签: laravel ionic-framework push-notification messaging


    【解决方案1】:

    我还使用以下代码在 laravel 中集成了推送通知,希望对您有所帮助:

    function sendPushNotification($fcm_token, $title, $message, $id="") {  
            $push_notification_key = Config::get('settings.PUSH_NOTIFICATION_KEY');    
            $url = "https://fcm.googleapis.com/fcm/send";            
            $header = array("authorization: key=" . $push_notification_key . "",
                "content-type: application/json"
            );    
    
            $postdata = '{
                "to" : "' . $fcm_token . '",
                    "notification" : {
                        "title":"' . $title . '",
                        "text" : "' . $message . '"
                    },
                "data" : {
                    "id" : "'.$id.'",
                    "title":"' . $title . '",
                    "description" : "' . $message . '",
                    "text" : "' . $message . '",
                    "is_read": 0
                  }
            }';
    
            $ch = curl_init();
            $timeout = 120;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
            // Get URL content
            $result = curl_exec($ch);    
            // close handle to release resources
            curl_close($ch);
    
            return $result;
        }
    

    【讨论】:

    • 只有fcm就够了吗?
    • 另外我们需要 push_notification_key,我已经在我的项目中使用了它,并且它与 FCM 和推送通知密钥(即项目 ID)一起工作正常
    • 项目ID是什么?
    • 当在firebase中注册我们的项目以进行推送通知时,会生成项目id,我们将在这里使用它。我们可以说FCM和Push Notification Key(即firebase项目id)
    • 这个结构在ios或者android上使用会不会很麻烦?
    【解决方案2】:
    function sendPushNotification($fcm_token, $title, $messages, $id="") {  
            $push_notification_key = Config::get('settings.PUSH_NOTIFICATION_KEY');    
            $url = "https://fcm.googleapis.com/fcm/send";            
            $header = array("authorization: key=" . $push_notification_key . "",
                "content-type: application/json"
            );    `enter code here`
    
            $postdata = '{
                "to" : "' . $fcm_token . '",
                    "notification" : {
                        "title":"' . $title . '",
                        "text" : "' . $messages . '"
                    },
                "data" : {
                    "id" : "'.$id.'",
                    "title":"' . $title . '",
                    "description" : "' . $messages . '",
                    "text" : "' . $messages . '",
                    "is_read": 0
                  }
            }';
    
            $ch = curl_init();
            $timeout = 120;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
            // Get URL content
            $result = curl_exec($ch);    
            // close handle to release resources
            curl_close($ch);
            return $result;enter code here
        }
    

    【讨论】:

      猜你喜欢
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2017-02-23
      • 2021-08-20
      相关资源
      最近更新 更多