モバイルデバイス用の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);
}