2016-10-16 4 views

答えて

0

JavaScriptを読むことができる場合は、simple-fcm-client example on react-native-fcmを確認してください。 Specifficaly、FirebaseClient.jsファイル

私は完全を期すため、ここでのコードを投稿しますが、あなたはあなたのFirebaseコンソールから、あなたのAPIのWebキーと運命のデバイスの登録トークンとFirebaseConstants.KEYでtokenを交換する必要があります。

const API_URL = "https://fcm.googleapis.com/fcm/send"; 

class FirebaseClient { 

    constructor() { 
    this.sendData = this.sendData.bind(this); 
    this.sendNotification = this.sendNotification.bind(this); 
    this.sendNotificationWithData = this.sendNotificationWithData.bind(this); 
    } 

    sendNotification(token) { 
    let body = { 
     "to": token, 
     "notification":{ 
      "title": "Simple FCM Client", 
      "body": "This is a notification with only NOTIFICATION.", 
      "sound": "default", 
      "click_action": "fcm.ACTION.HELLO" 
     }, 
     "priority": 10 
    } 

    this._send(JSON.stringify(body), "notification"); 
    } 

    sendData(token) { 
    let body = { 
     "to": token, 
     "data":{ 
      "title": "Simple FCM Client", 
      "body": "This is a notification with only DATA.", 
      "sound": "default", 
      "click_action": "fcm.ACTION.HELLO", 
      "remote": true 
     }, 
     "priority": "normal" 
    } 

    this._send(JSON.stringify(body), "data"); 
    } 

    sendNotificationWithData(token) { 
    let body = { 
     "to": token, 
     "notification":{ 
      "title": "Simple FCM Client", 
      "body": "This is a notification with NOTIFICATION and DATA (NOTIF).", 
      "sound": "default", 
      "click_action": "fcm.ACTION.HELLO" 
     }, 
     "data":{ 
      "title": "Simple FCM Client", 
      "body": "This is a notification with NOTIFICATION and DATA (DATA)", 
      "click_action": "fcm.ACTION.HELLO", 
      "remote": true 
     }, 
     "priority": "high" 
    } 

    this._send(JSON.stringify(body), "notification-data"); 
    } 

    _send(body, type) { 
    let headers = new Headers({ 
     "Content-Type": "application/json", 
     "Content-Length": parseInt(body.length), 
     "Authorization": "key=" + FirebaseConstants.KEY 
    }); 

    fetch(API_URL, { method: "POST", headers, body }) 
     .then(response => console.log("Send " + type + " response", response)) 
     .catch(error => console.log("Error sending " + type, error)); 
    } 

} 
+0

私のAndroidアプリ内でこのファイルを使用する必要がありますか? – Paul

+0

このファイルはプロジェクトに入れるのではなく、アイデアを伝えるためのものです。 AndroidにはフェッチAPIもありますので、上記のコードを変更することは非常に簡単です。 –

関連する問題