I wan’t to create an app that can forward the notification from one device to another using fcm.
The app will run on one device and listen the notification by NotificationListenerService as server, and now I can read the notification that posted on this device, but it seems it can not succeed in posting the notification info use Firebase Admin SDK.
I’m sure I can successful send the notification on another device as client, for I’ve tested using the offical tool on the Firebase control panel.
Firebase control panel
Here is my part of code:
“
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
val notifatication = sbn?.notification;
val extras = notifatication?.extras;
var extraImage = extras?.getString(Notification.EXTRA_PICTURE)
val title = extras?.getString(Notification.EXTRA_TITLE, "")
val body =
extras?.getCharSequence(Notification.EXTRA_TEXT, "").toString()
val token = sharedPreferences.getString("Token", "")
if(!token.isNullOrEmpty()){
Log.d(TAG, "send to $token")
val msgId = AtomicInteger()
val RMBuilder = RemoteMessage.Builder("[email protected]/fcm/send")
RMBuilder.setMessageId(msgId.incrementAndGet().toString())
RMBuilder.addData("title", title)
RMBuilder.addData("body", body)
// Send a message to the device corresponding to the provided
// registration token.
val response = FirebaseMessaging.getInstance().send(RMBuilder.build())
Log.d(TAG, "result is $response")
}
}
“
Here is my full code:
(https://github.com/HappyMax0/NotificationSync)
I’ve tried the Firebase Admin SDK, or is there any other way to send notifications use fcm?
Happy Max0 HappyMax0 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.