Created
February 15, 2021 14:00
-
-
Save abircse/fb259be780e818970ce0b29e0c5764b7 to your computer and use it in GitHub Desktop.
MyFireBaseMessagingService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFireBaseMessagingService : FirebaseMessagingService() { | |
var mtitle: String? = null | |
var mmessage: String? = null | |
override fun onMessageReceived(remoteMessage: RemoteMessage) { | |
super.onMessageReceived(remoteMessage) | |
mtitle = remoteMessage.data["title"] | |
mmessage = remoteMessage.data["message"] | |
showNotificationMessage(mtitle, mmessage) | |
} | |
private fun showNotificationMessage(title: String?, message: String?) { | |
var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) | |
{ | |
// Send Notification Data to Activity | |
val intent = Intent(this, MainActivity::class.java) | |
intent.putExtra("Title", title) | |
intent.putExtra("Message", message) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | |
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) | |
val CHANNEL_ID = "foodorderapp" | |
val notification = NotificationCompat.Builder(this) | |
.setSmallIcon(your_drawable_notification_icon) | |
.setContentTitle(title) | |
.setContentText(message) | |
.setContentIntent(pendingIntent) | |
.setStyle(NotificationCompat.BigTextStyle().bigText(message)) | |
.setAutoCancel(true) | |
.setChannelId(CHANNEL_ID).build() | |
val notificationChannel = NotificationChannel(CHANNEL_ID, "com.coxtunes.learnfirebase", NotificationManager.IMPORTANCE_HIGH) | |
mNotificationManager.createNotificationChannel(notificationChannel) | |
mNotificationManager.notify(RandomIntGenerator.generate(), notification) | |
} | |
else | |
{ | |
val intent = Intent(this, MainActivity::class.java) | |
intent.putExtra("Title", title); | |
intent.putExtra("Message", message); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | |
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) | |
val builder = NotificationCompat.Builder(this) | |
.setSmallIcon(your_drawable_notification_icon) | |
.setContentTitle(title) | |
.setContentIntent(pendingIntent) | |
.setContentText(message) | |
.setStyle(NotificationCompat.BigTextStyle().bigText(message)) | |
.setAutoCancel(true) | |
mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
mNotificationManager.notify(RandomIntGenerator.generate(), builder.build()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment