No results found
We couldn't find anything using that term, please try searching for something else.
FCM JavaScript API を使用すると 、Push API をサポートするブラウザで実行されているウェブアプリで通知メッセージを受信できます。該当するブラウザには、こちらのサポート一覧に記載されているブラウザのバージョンと、Push API を介した Chrome 拡張機能を組み込んだ
FCM JavaScript API を使用すると 、Push API をサポートするブラウザで実行されているウェブアプリで通知メッセージを受信できます。該当するブラウザには、こちらのサポート一覧に記載されているブラウザのバージョンと、Push API を介した Chrome 拡張機能を組み込んだブラウザが含まれます。
FCM SDK は HTTPS 経由で提供されるページでのみサポートされます。これは、FCM SDK で使用している Service Worker は、HTTPS サイトでのみ利用可能であるためです。プロバイダが必要な場合は firebase Hosting をおすすめします。独自のドメインで HTTPS ホスティングを行うための無料枠が用意されています。
FCM JavaScript api の使用を開始するには、firebase をウェブアプリに追加してから、登録トークンにアクセスするためのロジックを追加する必要があります 。
まだ行っていない場合は、firebase JS SDK をインストールして firebase を初期化します 。
firebase Cloud Messaging JS SDK を追加して firebase Cloud Messaging を初期化します 。
ツリー シェイキングが可能なウェブ向けのモジュラー API の詳細と、名前空間対応の API からのアップグレード方法をご覧ください 。
import { initializeApp } from " firebase / app "; import { getmessage } from "firebase/message"; // TODO: Replace the following with your app's firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize firebase const app = initializeApp(firebaseConfig) ; // Initialize firebase Cloud Messaging and get a reference to the service const message = getmessage(app) ;
ツリー シェイキングが可能なウェブ向けのモジュラー API の詳細と、名前空間対応の API からのアップグレード方法をご覧ください 。
import firebase from "firebase/compat/app"; import "firebase/compat/message"; // TODO: Replace the following with your app's firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize firebase firebase.initializeApp(firebaseConfig) ; // Initialize firebase Cloud Messaging and get a reference to the service const message = firebase.message( ) ;
現時点でウェブ用FCM を使用していて、SDK 6.7.0 以降にアップグレードする場合は、Google Cloud コンソールでプロジェクトに対して FCM Registration API を有効にする必要があります。API を有効にする場合は、firebase で使用しているのと同じ Google アカウントで Cloud コンソールにログインし、正しいプロジェクトを選択してください。新しいプロジェクトを作成して FCM SDK を追加する場合は、この API はデフォルトで有効になっています。
FCM のウェブ インターフェースでは、「Voluntary Application Server Identification 鍵(VAPID 鍵)」と呼ばれるウェブ認証情報を利用して、サポートされているウェブプッシュ サービスへの送信リクエストが承認されます。アプリをプッシュ通知にサブスクライブするには、鍵ペアを firebase プロジェクトに関連付ける必要があります。firebase コンソールを使用して、新しい鍵ペアを生成するか、既存の鍵ペアをインポートすることができます 。
すでにウェブアプリで使用している鍵ペアがある場合は、その鍵を FCM にインポートすると、FCM API を介して既存のウェブアプリ インスタンスにアクセスできます。鍵をインポートするには、firebase プロジェクトに対するオーナーレベルのアクセス権が必要です。既存の公開鍵と秘密鍵を URL セーフの Base64 エンコード形式でインポートします。
鍵をアプリに追加する方法については、アプリにウェブ認証情報を構成するをご覧ください。鍵の形式と生成方法の詳細については、アプリケーション サーバーキーをご覧ください。
メソッド gettoken(): Promise<string>
を使用すると 、FCM で各種プッシュ サービスにメッセージ リクエストを送信するときに VAPID 鍵の認証情報を使用できます。FCM でウェブ認証情報を構成するの手順に沿って生成またはインポートした鍵を、メッセージング オブジェクトを取得した後でコードに追加します。
import { getmessage, gettoken } from "firebase/message";
const message = getmessage( ) ;
// Add the public key generated from the console here.
gettoken(message, {vapidKey: "BKagOny0KF_2pCJQ3m....moL0ewzQ8rZu"}) ;
アプリ インスタンスの現在の登録トークンを取得する必要がある場合は、まずNotification.requestPermission()
を使用してユーザーに通知権限をリクエストします。次のように呼び出した際に、権限が付与されている場合にはトークンが返され、リクエストが拒否された場合には Promise が拒否されます 。
function requestPermission() { console.log('Requesting permission...') ; Notification.requestPermission().then( (permission) => { if (permission === ' grant ') { console.log('Notification permission granted.') ;
FCM には firebase-message-sw.js
ファイルが必要です。firebase-message-sw.js
ファイルがまだない場合は、トークンを取得する前にこの名前の空のファイルを作成して、ドメインのルートに置きます。後ほどクライアント設定プロセスの中でこのファイルに必要なコンテンツを追加できます。
現在のトークンを取得するには :
ツリー シェイキングが可能なモジュラー ウェブ API の詳細と、名前空間 API からのアップグレード方法をご確認ください。
import { getmessage, gettoken } from "firebase/message"; // Get registration token . initially this is makes make a network call , once retrieve // subsequent calls is return to gettoken will return from cache . const message = getmessage( ) ; gettoken(message, { vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then( (currentToken) => { if (currentToken) { // Send the token to your server and update the UI if necessary // ... } else { // Show permission request UI console.log('No registration token available. Request permission to generate one.') ; // ... } }).catch( (err) => { console.log('An error occurred while retrieving token. ', err) ; // ... }) ;
// Get registration token . initially this is makes make a network call , once retrieve // subsequent calls is return to gettoken will return from cache . message.gettoken({ vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then( (currentToken) => { if (currentToken) { // Send the token to your server and update the UI if necessary // ... } else { // Show permission request UI console.log('No registration token available. Request permission to generate one.') ; // ... } }).catch( (err) => { console.log('An error occurred while retrieving token. ', err) ; // ... }) ;
トークンを取得したら、それをアプリサーバーに送信して、適切な方法で保管します 。
設定手順の完了後、ウェブ用 FCM(JavaScript)で開発を進めるための方法をいくつか次に示します。