微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我的应用程序在后台时如何显示通知

如何解决当我的应用程序在后台时如何显示通知

我有一个 Laravel 应用程序使用名为 laravel-fcm-notification 的包通过 firebase 云消息传递发送推送通知

当发送消息时,Laravel 应用程序一切正常,我收到了这样的回复

Application#onCreate

封装代码

{
    "multicast_id" => 524702081778807088
    "success" => 1
    "failure" => 0
    "canonical_ids" => 0

}

安卓主节

public function toFcm($notifiable)
    {
        $message = new FcmMessage();
        $message->setHeaders([
            'project_id' => "904447526427",// FCM sender_id 904447526427
        ])->content([
            'title' => 'Invoice Approved','body' => 'test message from laravel package',])->data([
            'title' => "Salesman {$notifiable->name} requesting for  discount",'image' => 'placeholder.jpg ','message' => 'You got a new Message',])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

        return $message;

    }

收到通知的android函数

<service
    android:name=".utils.services.MessagingController"
    android:permission="signature"
    android:stopWithTask="true"
    android:enabled="true"
    android:exported="true"
    tools:ignore="Instantiatable">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

当我的前台不在后台通知来了? 有什么帮助吗?

解决方法

我花了一些时间浏览代码才弄明白。

another SO answer中所述,您可以发送两种类型的消息:

  1. 显示消息:这些消息仅在您的应用处于前台时触发 onMessageReceived() 回调
  2. 数据消息s:即使您的应用处于前台/后台/终止状态,这些消息也会触发 onMessageReceived() 回调

您的问题似乎与此信息有关。来自同一答案的附加引用:“注意:请确保您没有添加 JSON 密钥通知”。


有了这些信息,我们可以仔细研究您在 FcmMessage 上调用的两个可能相关的方法:content()data()

参见line 101

public function content(array $params)
{
    $this->notification = $params;

    return $this;
}

然后在 line 282 中调用的 formatData() (FcmChannel) 中,我们遇到了这个:

if (isset($this->notification) && count($this->notification)) {
    $payload['notification'] = $this->notification;
}

从这里我会说可以安全地假设有效负载在发送之前是 JSON 编码的。因此,您要添加 JSON 密钥“通知”,根据我之前链接的 SO 答案,您不应该这样做。

试试这样的:

public function toFcm($notifiable)
    {
        $message = new FcmMessage();
        $message->setHeaders([
            'project_id' => "904447526427",// FCM sender_id 904447526427
        ])->data([
            'title' => 'Invoice Approved','body' => 'test message from laravel package',])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

        return $message;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。