0

Can anyone help me about notification with Firebase to learn good things. For me, I think the sendRegistrationToServer() method is not necessary when the token is updated.

In all cases, the notification will be sent to all the devices where the application is located.

What is the utility of sendRegistrationToServer() method?

@Override
public void onTokenRefresh() {
  String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  //I talked about this method. :)
  sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
  // Add custom implementation, as needed.
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • There is no way to send to *all* devices through Firebase Cloud Messaging or Firebase Notifications. See http://stackoverflow.com/questions/38427525/how-to-send-notifications-to-all-devices-using-firebase-cloud-messaging/38427849#38427849 – Frank van Puffelen Jul 23 '16 at 03:17

2 Answers2

4

A Token is used as a means to identify your device. In simple android app using FCM (like in the tutorial and firebase-chat), guessing from your code (correct me if I'm wrong), we don't need to send the token to server because we don't implement our server to send the message. We directly talk with google FCM server.

When we implementing our own server to send the notification message, we need to know with high confident that our server 'talk' with a valid device. So it use the token generated from device as identifier.

Server will send the notification with your token to FCM server (i.e Google Server), then the FCM Server will handle the sending process.

So you only need to construct sendRegistrationToServer(refreshedToken); when you want to authorize your app receiving notification from your server.


let's explain it more with CodePath Google Cloud Messaging tutorial:

Much of the heavy lifting in supporting push notifications on Android is facilitated by Google-powered connection servers. These Google servers provide an API for messages to be sent from your server and relay these messages to any Android/iOS devices authorized to receive them.

An Android device with Google Play Services will already have FCM client support available. For push notifications to be received, an app must first obtain a token by registering with a Google server:

token

This token then must be passed along to your server so that it can be used to send subsequent push notifications:

registration

Push notifications can be received assuming your app has registered to listen for FCM-based messages:

enter image description here

In other words, in order to implement FCM, your app will need both a Google server and your own server. When your app gets a token from Google, it needs to forward this token to your server. This token should be persisted by the server so that it can be used to make API calls to the Google server. With this approach, your server and the Android device do not need to create a persistent connection and the responsibility of queuing and relaying messages is all handled by Google's servers.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • So, i can use only google FCM server to send Notification to devices without implementing our own server ? Thank you for coming and sharing your special time with me. –  Jul 23 '16 at 01:21
  • Yes, that's correct. I'm just trying to contribute to SO where previously I'm just a cool silent user. Keep up the good work ;) – ישו אוהב אותך Jul 23 '16 at 01:28
1

The notification will be sent to a device indentified by a token, so if your token gets updated and you don't update your server with the new token, then when the your server use the old token your notification will go nowhere. So you should update your server every time your token gets refreshed.

josemgu91
  • 719
  • 4
  • 8