3
votes

Yesterday Google has announced a new set of tools for Firebase, one of them was Notifications the ability to send notifications from server to devices which are using my app.

But can we now notify users when they receive a new message?

And if not, is there a way around to achieve this?

2
If you mean a new message from another user then I was also wondering this. Looking at the documentation, it looks like this is still not possible automatically. I think you'd need to have another server check for each messages sent and then create a notification tagged with the recipient user's ID. Then you subscribe the recipient to it's own user ID tag. Check link - user2997154
Great hunting in the docs @user2997154. If you write it up as an answer, I'll upvote. :-) - Frank van Puffelen

2 Answers

5
votes

Looking at the documentation, this doesn't seem currently possible automatically. Here is a possible way to accomplish it "manually" with another server:

  1. Subscribe a user to it's own user ID

In android

FirebaseMessaging.getInstance().subscribeToTopic("InsertUserIDHere");

In IOS

[[FIRMessaging messaging] subscribeToTopic:@"/topics/InsertUserIDHere"];
  1. Setup an outside server that checks every sent messages. When a message is sent, the server should create a notification that includes the recipient's user ID as the topic.

Look here for more info.

1
votes

You can send messages to group of users (targeting a specific "topic") or to a single device (targeting a Firebase Cloud Messaging token).

To subscribe a device to a topic use:

FirebaseMessaging.getInstance().subscribeToTopic("topicName");

To obtain the device token use (*1) :

FirebaseInstanceId.getInstance().getToken();

Then you can use the Firebase Notificaitons web console, or the FCM server API if you want to send messages from your server.
See: https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server


Notes:

[1] getToken() can return null if the token is not yet available.
You can use the callback onTokenRefresh() to be notified when the token is available and when the token is rotated.
See: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register