【问题标题】:Append messages to push notification - Firebase将消息附加到推送通知 - Firebase
【发布时间】:2018-06-01 09:07:50
【问题描述】:

我正在开发类似 WhatsApp 的网络应用程序,并希望通知用户有新消息。我正在使用 Firebase 的推送通知。

如果用户收到 message1,则会显示“message1”的通知。 当消息 2 出现时,我想将其附加到上一个通知的正文中,而不是显示新的通知。

如“消息 1 \n 消息 2”等。

一旦通过单击读取通知并且当有新消息进来时说“Message3”,它应该是正文中的新消息,没有已阅读的Message1和Message2。

我发送通知的代码如下所示。



    define( 'API_ACCESS_KEY', 'AAAAlHKlaCE_______________________-NdJXkojLf9Ap9mu' );
    $data = array("to" => $key,
    "priority" => "normal",
    "collapseKey" => "demo",
    "notification" => array( 
    "title" => "New message",
    "body" => "message1 ",
    "icon" => "../profiles/p1.jpg",
    "tag" => "hello",
    "click_action" => "http://website.com"
    ));                                                                    
    $data_string = json_encode($data); 
    echo "The Json Data : ".$data_string; 
    $headers = array
    (
    'Authorization: key=' . API_ACCESS_KEY, 
    'Content-Type: application/json'
    );                                                                                 
    $ch = curl_init();  
    curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );                                                                  
    curl_setopt( $ch,CURLOPT_POST, true );  
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, $data_string);                                                                  
    $result = curl_exec($ch);
    curl_close ($ch);

就像 whatsapp 一样,将对话的新消息添加到通知的正文中。 如何使用 firebase 来做到这一点?

【问题讨论】:

  • 为什么这个帖子有两个反对票?
  • 问题的格式可以稍微多一点,以便于阅读和理解。但是,是的,在不评论需要纠正的地方的情况下投反对票是很糟糕的。你得到了我的投票和答案。希望对您有所帮助。
  • 我已经改写了这个问题。看看它既满足你的需要,如果不满足,那就更正。

标签: firebase web-applications push-notification notifications progressive-web-apps


【解决方案1】:

“标记和重新通知”是您必须使用的两件事来实现您想要的。 Code example here 在这两个标题下。

   const title = 'My Messenger';
    const options = {
      body: 'Message 1 \n Message 2',
      tag: 'MyMessengerGroup'
    };
    registration.showNotification(title, options);

每次收到新消息时,请使用上面突出显示的相同标签名称。这将确保它会覆盖相同的通知,但会使用您附加的消息,如下所示。

   const title = 'My Messenger';
    const options = {
      body: 'Message 1 \n Message 2 \n Message3',
      tag: 'MyMessengerGroup'
    };
    registration.showNotification(title, options);

使用消息数组来存储所有传入的消息。如果用户单击了通知,您可以按照代码示例链接中的说明使用“通知单击事件”来清除用于构建通知正文的“消息数组”。

【讨论】:

  • 你解决问题的方式是,我必须记录之前发送的所有消息,这些消息必须附加到新消息中。然而,我不想要那个。我希望 firebase 记录旧消息。但我发现这是不可能的。因此,我使用数据库将所有以前的消息存储在我的数据库中,并将其附加到新消息中。谢谢你的帮助。
猜你喜欢
  • 2020-06-05
  • 1970-01-01
  • 2017-10-04
  • 2016-12-13
  • 1970-01-01
  • 2020-05-09
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多