I am trying to establish push notifications in my app for certain actions ("someone liked your this", "someone commented on that" etc).
I tried to follow google's guide here
But when I copied the code, this part of it
String response = FirebaseMessaging.getInstance().send(message);
Gave an error saying it is expecting a RemoteMessage
and not Message
. I am trying to change it to RemoteMessage, but I can't understand:
.setToken
.RemoteMessage.Builder()
I've been going through a lot of manuals but I can't find any that address it. Is it a new change in the API?
The best way to help me would be an example with Kotlin of a new generated message - one that in response would trigger a new push notification for the recipient, based on the token (I'm pretty sure I can handle the receiving by myself).
Thank you!
Edit:
I was able to make it fit the pattern it would be happy with, but I can't get the notifications to show up.
This is the function I am using to send the notification:
fun sendCloudMessage(userId: String) {
val receiverRef = FirebaseDatabase.getInstance().getReference("/users/$userId/services/firebase-token")
receiverRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
val registrationToken = p0.getValue(String::class.java)
Log.d("tokencomplete", p0.toString())
Log.d("tokenonly", registrationToken)
val fm = FirebaseMessaging.getInstance()
fm.send(
RemoteMessage.Builder("$registrationToken@gcm.googleapis.com")
.setMessageId(Integer.toString(0))
.addData("my_message", "Hello World")
.addData("my_action", "SAY_HELLO")
.build()
)
}
})
}
The userId
the I pass to this function is the firebase uid, and using that I fetch the most recent token of the app the user is using. I've checked and that part works and I get in return the token in a format similar to this
ecK86WRoAh4:APA91bGQWSesCptPF_nzfkmgDe-zOYYo2Cj4XT_NeNTVaBxQakbvauOpkVFd7DCRIugMCfso5SadEUtyTRvrhdCiBpKxGGMRgUYNsUhA24f9IkmxjL5dWD
But the function itself doesn't spark any notification in the other device (I am using two devices trying to get one to spark a notification with the other).
Set the sender id in your RemoteMessage
builder constructor, it should look like this:
RemoteMessage.Builder(token)
;
edit
here is the method im using:
public void sendMessage (final String data) {
FirebaseMessaging fm = FirebaseMessaging.getInstance();
String token = FirebaseInstanceId.getInstance().getToken();
String msgID = DigestUtils.sha1Hex(token + System.currentTimeMillis());
String SENDER_ID = "someID";
RemoteMessage.Builder RMBuilder =
new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com");
RMBuilder.setMessageId(msgID);
Map<String, Object> mapData = Utils.jsonToMap(data);
for (Map.Entry<String, Object> entry : mapData.entrySet()) {
RMBuilder.addData(entry.getKey(), entry.getValue().toString());
}
fm.send(RMBuilder.build());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.