2017-10-03 9 views
0

モバイルデバイス用のfirebaseおよびnotificacionプッシュメッセージを使用しています。私は1つのプロジェクトと2つのアプリケーション(IOS - ANDROID)でfirebaseコンソールを設定しました。 firebase consoleを使用して通知を送信しようとすると、そのメッセージがデバイスに表示されますが、httpプロトコルとPOSTメソッドを使用すると、レスポンスは{"multicast_id":******、 "成功":0、 "失敗":1、 "canonical_ids":0、 "results":[{"error": "InvalidRegistration"}]}そして、私はメッセージが電話に送られないので、無効な登録に関するエラーを見つける方法を知らない。私はSERVER_KEYとアプリケーションのためのトークンをコピーして、私は、サーバーからこれをsetted:モバイルデバイスでJavaおよびCordova-Phonegapを使用したHTTPおよびPOSTメソッドでプッシュ通知が機能しない

String title = "TITLE"; 

String message = "THIS IS A MESSAGE"; 

String APP_ID[] = { 
       "1:********:ios:*******", 
       "1:*******:android:*****" 
}; 

String SERVER_KEY="***************....****"; 

String SERVER_URL = "https://fcm.googleapis.com/fcm/send"; 

String json = "{" 
     + "\"notification\":{" 
     + "\"title\": \""+title+"\"," 
     + "\"body\": \""+message+"\"," 
     + "\"sound\": \"default\"" 
     + "}," 
     + "\"content_available\": true," 
     + "\"priority\": \"high\"," 
     + "\"to\": \""+APP_ID[1]+"\"" 
     + "}"; 

StringEntity entity = new StringEntity(json); 

CloseableHttpClient client = HttpClientBuilder.create().build(); 

HttpPost request = new HttpPost(SERVER_URL); 
request.addHeader("Content-Type", "application/json"); 
request.addHeader("Authorization", "key="+SERVER_KEY); 
request.setEntity(entity); 
HttpResponse response = client.execute(request); 

responseCode = response.getStatusLine().getStatusCode(); 

System.out.println(EntityUtils.toString(response.getEntity())+". Status Code: "+responseCode); 

client.close(); 

設定はPhoneGapの-pluginのプッシュを使用してJavascriptのです。私がCordova APPを実行すると、コンソールに成功登録が表示されます。 グーグル-services.jsonとGoogleServices-Info.plistファイルは、コマンドを使用して、インストールプラグイン時に構成された:

$> cordova plugin add phonegap-plugin-push --variable SENDER_ID = 'SENDER_CODE_extract_from_firebase_project' 

とJavaScriptの設定:

var app = { 
bindEvents: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false); 
}, 
onDeviceReady: function() { 
    app.receivedEvent('deviceready'); 

     PushNotification.hasPermission(permissionListener); 

     var push = PushNotification.init({ 
         android: { 
          vibrate: true, 
          sound: true 
         }, 
         ios: { 
          alert: true, 
          sound: true 
         } 
        }); 

     push.on('error',errorListener); 

     push.on('registration',registrationListener); 

     push.on('notification', notificationListener); 

    } 
}; 



//FOR PUSH NOTIFICATIONS 

    /** 
    * Listen if the device has permissions. 
    */ 
    function permissionListener(data) { 

     console.log('is enabled: '+data.isEnabled); 

    } 

    /** 
    * For registration event 
    */ 
    function registrationListener(data){ 
     console.log("Registration id: "+data.registrationId); 
     console.log("Registration Type: "+data.registrationType); 
    } 


    function notificationListener(data){ 
      alert(data.message); 
    } 

    function errorListener(e){ 
     console.log("ERROR with PUSH NOTIFICATIONS: "+e.message); 
    } 

答えて

0

まあ、私の問題は、 "APPS_ID" を変更修正されましたベクトル値。私はJSONの "to"フィールドのIDについて混乱しました。 APP IDはメッセージを送信する正しいトークンではありません。 アプリコードを実行すると、プッシュ通知のプラグインに登録IDが表示され、正しいトークンになります。

0

電話で受信した間違ったdeviceIdをサーバーに送信していますが、deviceIdを変更または変更しないでください。

Invalid Registration Token 200 + error:InvalidRegistration Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

https://firebase.google.com/docs/cloud-messaging/http-server-ref

関連する問題