2

リファレンス動作しません:https://github.com/firebase/quickstart-js/tree/master/messagingFirebaseクラウドメッセージングrequireInteractionは

を私はキーと値のペア追加しました:

"requireInteraction": true 

をしかし、デスクトップChromeの通知はまだ20秒後に消えます。 Firebaseがこのキーと値のペアをサポートしているかどうかは知っていますか?ありがとう!

以下の私の例です。あなたに[...]を変更してください。

curl -X POST -H "Authorization: key=[...]" -H "Content-Type: application/json" -d '{ 
    "notification": { 
    "requireInteraction": true,  
    "title": "This is custom title", 
    "body": "this is custom body", 
    "click_action": "https://google.com", 
    "data" : {"requireInteraction": true } 
}, 
    "to": "[...]", 
}' "https://fcm.googleapis.com/fcm/send" 
+0

こんにちはサミュエル:これはあなたのカールの呼び出しだろう

// import scripts omitted const messaging = firebase.messaging(); // [END initialize_firebase_in_sw] self.addEventListener('notificationclick', e => { let found = false; let f = clients.matchAll({ includeUncontrolled: true, type: 'window' }) .then(function (clientList) { for (let i = 0; i < clientList.length; i ++) { if (clientList[i].url === e.notification.data.click_action) { // We already have a window to use, focus it. found = true; clientList[i].focus(); break; } } if (! found) { clients.openWindow(e.notification.data.click_action).then(function (windowClient) {}); } }); e.notification.close(); e.waitUntil(f); }); // [START background_handler] messaging.setBackgroundMessageHandler(function (payload) { console.log('[firebase-messaging-sw.js] Received background message ', payload); // Customize notification here return self.registration.showNotification(payload.data.title, Object.assign({data: payload.data}, payload.data)); }); // [END background_handler] 

を:ここでは、あなたのセットnotificationていきまさにんが、代わりにデータプロパティを使用して作業サービスワーカーです。私は[requireInteraction'](https://developers.google.com/web/updates/2015/10/notification-requireInteraction)があなたのペイロードに設定すべきものではないと思います。あなたが*通知をビルドしているときに宣言されるべきです。質問だけがサポートされている場合、 'requireInteraction'は' notification'ペイロードに属しませんが、 'data'ペイロードに*存在することができます。 :) –

+0

@AL。私はrequireInteractionを追加しようとしました:データペイロードまたは通知ペイロードにtrueを加えましたが、クロームデスクトップの通知ポップアップはまだ20秒後に消えます。 –

+0

以下のデモのポップアップの1つは、ユーザーがクリックするまで閉じることはありません。 https://googlechrome.github.io/samples/notifications/requireInteraction.html –

答えて

2

Firebaseは、メッセージが配信されるnotificationペイロードからrequireInteraction性ストリップ。回避策は、notificationの代わりにdataプロパティを使用することです。あなたはそれになりたいとあなたは、通知を構築するためにsetBackgroundMessageHandler()メソッドを使用することができます。

messaging.setBackgroundMessageHandler(function (payload) { 
    return self.registration.showNotification(payload.data.title, 
     Object.assign({data: payload.data}, payload.data)); 
}); 

click_actionは、もはやこの方法で動作し、あなたが希望のonclickハンドラを登録する必要がありますので、私は、上記のdataを設定されていませんしましたあなた自身。

curl -X POST -H "Authorization: key=yourKey-" -H "Content-Type: application/json" -d '{ 
"data": { 
    "title": "fooTitle", 
    "body": "foo", 
    "icon": "image.jpg", 
    "click_action": "http://localhost:8000", 
    "requireInteraction": true 
    }, 
    "registration_ids": ["id1", "id2"] 
}' "https://fcm.googleapis.com/fcm/send" 
+0

これはまだ正しいですが、通知を維持することができないようですか?私はfirebase 4.3.0を使用しています。上記のcurlリクエストと唯一の違いは、まだ通知ペイロードがあることです。データと通知の両方があります。 – virtualLast

+1

私はあなたが1つを決定する必要があると思います。正しく覚えていれば、通知が優先され、通知ペイロードがない場合にのみデータが使用されます。 – baao

+0

素晴らしいおかげで、芽 - 通知のものを削除し、それは魅力のように働いた – virtualLast

関連する問題