Question:
To send push notifications using FCM, you need an app server, I'm completely new to programming and all I can do is write a little in Java and don't understand the concept of servers.
What kind of server is it, where it should be registered, in the application, or is it something that should communicate with the application from outside? There is a lot of information on the Internet about what needs to be registered in the server, but all these examples assume that I have a general idea of what a server is and what I have. Also from what I read, it follows that this server needs to be written not in java, but for example in php, if so, how can I connect this in my application?
I myself am surprised at what a stupid question it turned out, but I can’t even formulate it normally, because I don’t understand what this server is.
In general, I will be grateful for any information on what is the app server used when working with FCM on android.
Answer:
You correctly understood that the App server
needed in order to communicate with the application from outside. When you receive a token
in your application, you send it to the server. Thus, the server can send you push-notifications
using this token.
Here's an example in Node js
:
var gcm = require('node-gcm');
var API_KEY = "yourApiKey";
var sender = new gcm.Sender(API_KEY);
//объект сообщения
var message = new gcm.Message({
collapseKey: 'data',
priority: 'high',
contentAvailable: true,
delayWhileIdle: false,
timeToLive: 1000
data: {
message: 'Message from gcm server',
action: 'Update data on server'
},
notification: {
tag : 'hasData',
title: "Title text",
icon: "ic_launcher",
color: "#22C064",
sound: "notification_sound",
body: "This is a GCM notification that will be displayed ASAP.",
click_action: "OPEN_APP" // make intent-filter in Manifest.xml for this action
}
});
//Список токенов которым будут отправлены сообщения
var registrationTokens = [];
registrationTokens.push('owQMHz9-Ep6FtiB-pp9uFcZTKcdvUhrsG3XdL7IWgZSt8cWfASzPPxEW1cBdLn1OUukqfsk9rlTexO3MQ0EeSdLXaAFXQn7vYzrKG1LTnv8LOxkBQqEd0VnxLd4');
//отправить на конкретные девайсы
sender.send(message, { registrationTokens: registrationTokens }, function (err, response) {
if(err){
console.error(err);
} else{
console.log(response);
}
});
//отправить на все устройства которые подписаны на этот топик
sender.send(message, {to : "/topics/global" }, function (err, response) {
if(err){
console.error(err);
} else{
console.log(response);
}
});