【问题标题】:How to display Notification when my app is in a background当我的应用程序在后台时如何显示通知
【发布时间】:2021-01-17 11:50:21
【问题描述】:

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

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

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

    }

安卓主祭

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

收到通知的android函数

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    DLog("")
    val data = remoteMessage.data
    remoteMessage.notification?.let {
        data.set(NOTI_TITLE , it.title)
        data.set(NOTI_MESSAGE , it.body)
        //Add more data if necessary
    }
    pushNotification.show(applicationContext, data)
}

当我在前台时通知正在发送而不是在后台发送? 请问有什么帮助吗?

【问题讨论】:

    标签: php laravel firebase google-api-client


    【解决方案1】:

    这花了我一些时间浏览代码来弄清楚。

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

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

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


    有了这些信息,我们可以更深入地了解您在 FcmMessage 上调用的可能相关的两种方法:content()data()

    请参阅line 101 了解:

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

    然后在 formatData() (line 282) 中,在 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多