First of all FCM is used for push Notification. Not for internal communication.
<service android:name=".fcms.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
along with
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
val TAG :String = "MessagingService"
override fun onNewToken(str: String?) {
super.onNewToken(str)
//send token to server... if user is logged In...
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
/*Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());*/
// Check if message contains a data payload.
if (remoteMessage?.data!!.size > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
/*Log.e(TAG, "collapsekey: " + remoteMessage.getCollapseKey());
Log.e(TAG, "from: " + remoteMessage.getFrom());
Log.e(TAG, "message id: " + remoteMessage.getMessageId());
Log.e(TAG, "message type:: " + remoteMessage.getMessageType());
Log.e(TAG, "to: " + remoteMessage.getTo());
Log.e(TAG, "send time: " + remoteMessage.getSentTime());
Log.e(TAG, "ttl: " + remoteMessage.getTtl());
Log.e(TAG, "title: " + remoteMessage.getNotification().getTitle());
Log.e(TAG, "body: " + remoteMessage.getNotification().getBody());
Log.e(TAG, "click action: " + remoteMessage.getNotification().getClickAction());
Log.e(TAG, "color: " + remoteMessage.getNotification().getColor());
Log.e(TAG, "icon: " + remoteMessage.getNotification().getIcon());*/
}
// Check if message contains a notification payload.
if (remoteMessage.notification != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.notification!!.body!!)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val NOTIFICATION_CHANNEL_ID = "broadpeak.notification"
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val name = "my_channel"
val Description = "This is my channel"
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = Description
mChannel.enableLights(true)
mChannel.lightColor = Color.RED
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mChannel.setShowBadge(false)
notificationManager.createNotificationChannel(mChannel)
}
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
val tagID = remoteMessage?.data!!.get("tagID").toString()
val tagName = remoteMessage?.data!!.get("tagName").toString()
val intent = Intent(this, McqActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// | Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("keyTagBO",SubjectBO(tagID,tagName))
val resultIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val startIntent = PendingIntent.getService(this,0,
Intent(this,BackgroundService::class.java).setAction("START"),PendingIntent.FLAG_UPDATE_CURRENT)
val stopIntent = PendingIntent.getService(this,0,
Intent(this,BackgroundService::class.java).setAction("STOP"),PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.round_green_layout)
.setOngoing(true)//If you want not to remove it on Swipe
.setContentTitle(remoteMessage.notification!!.title)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(longArrayOf(0, 1000, 500, 1000))
.setContentText(remoteMessage.notification!!.body)
.setContentIntent(resultIntent)
.addAction(R.mipmap.ic_launcher,"Start Action",startIntent)
.addAction(R.mipmap.ic_launcher,"Stop Action",stopIntent)
.setContentInfo("Info")
notificationManager.notify(Random().nextInt(), notificationBuilder.build())
}
}
Go to firebase console and select Cloud Messaging and create a notification .. and pass key value pair if you like
