使用laravel

时间:2018-08-26 17:50:51

标签: php laravel ionic-framework laravel-5

我有一个离子移动应用程序,可以使用API​​链接到laravel网络应用程序。现在,以一种这样的方式设置后端,即当管理员向移动用户或一组用户发送消息时,推送通知应在带有令牌的注册设备上显示。 我正在使用此Laravel FCM package。 该代码是系统的一部分:

app.js(离子部分)

...
FCMPlugin.getToken(
            function (token) {
                //alert('Token: ' + token);
                console.log('Token: ' + token);
                localStorage.setItem("device_token", token);
            },
            function (err) {
                //alert('error retrieving token: ' + token);
                console.log('error retrieving token: ' + err);
            }
        );

        FCMPlugin.onNotification(
            function(data){

                if(data.wasTapped){
        //Notification was received on device tray and tapped by the user.

                  alert("Tapped: " +  JSON.stringify(data) );

                }else{
        //Notification was received in foreground. Maybe the user needs to be notified.
                    console.log("Not tapped: " + JSON.stringify(data) );


                }
 ...

ProcessMessage.php(laravel)

public function send()
{

    $message = $this->message;

    //Get message subscribers
    $subscribers = Subscriber::whereHas('groups', function ($query) use ($message) {
        $query->whereIn('groups.id', $message->groups()->get()->pluck('id')->toArray());
    })->get();

    if($this->message->type=='notification'){
        foreach ($subscribers as $subscriber) {
            if ($subscriber->user->status == 'active') {
                $subscriber->messageReads()->save(new MessageRead(['message_id' => $message->id, 'status' => '0']));
            }
        }
    $this->updateSendMessage($this->message);

    private function updateSendMessage($message){
    $message->status = '1';
    $message->authorized = '1';
    $message->save();
}

FCMService.php

    namespace App\Services;

    use App\Models\Message;
    use App\Models\SubscriberToken; // need help with this model
    use LaravelFCM\Message\OptionsBuilder;
    use LaravelFCM\Message\PayloadDataBuilder;
    use LaravelFCM\Message\PayloadNotificationBuilder;
    use FCM;

    class FCMService
   {
    public function sendFCMNotification(Message $message){


    $downstreamResponse = $this->sendMessageToTokens($message, $message->tokensArray());

    $downstreamResponse->numberSuccess();
    $downstreamResponse->numberFailure();
    $downstreamResponse->numberModification();

    $tokens_to_update = $downstreamResponse->tokensToModify();
    foreach ($tokens_to_update as $item=>$value){
        $entry = SubscriberToken::where('token', $item)->first(); // Not sure about this part. How to create SubscriberToken model
        $entry->token = $value;
        $entry->save();
    }

    $tokens_to_retry = $downstreamResponse->tokensToRetry();

    if(!empty($tokens_to_retry)){
        $downstreamResponse = $this->sendMessageToTokens($message, $tokens_to_retry);
    }

    return;
}

public function sendMessageToTokens(Message $message, $tokens){
    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60*20);

    $notificationBuilder = new PayloadNotificationBuilder($message->title);
    $notificationBuilder->setBody($message->short_content)->setSound('default');

    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData(['message' => $message]);

    $option = $optionBuilder->build();
    $notification = $notificationBuilder->build();
    $data = $dataBuilder->build();

    return $downstreamResponse = FCM::sendTo($tokens, $option,  $notification, $data);
     }
    }

MessageController.php

...
public function update($id, Request $request)
{
    $message = Message::findOrFail($id);
    if(!$this->clientHelper()->hasMessage($id)){
        flash()->error('You cant view that message');
        return back();
    }
    if($message->status=='1' or $message->authorize=='1'){
        flash()->error('Action not allowed, message already sent!!');
        return back();
    }

    if($request->authorize){
        if(auth()->user()->can('authorizeSend', $this->message_class)){
            $this->sendMessage($message);
            flash()->message("Message sent!");
            return back();
        }
    }
    ...
    public function sendMessage($message)
    {
    $messageProcessor = new ProcessMessage($message);
    $messageProcessor->send();
    }

我已经使用服务器密钥设置了Firebase控制台配置,并根据文档将其放置在laravel应用中。消息已由注册用户接收,但推送通知不起作用。请帮忙。

0 个答案:

没有答案