Created
November 27, 2018 13:22
-
-
Save Rafhack/c81f871ec09a6b74f37c3df3a72ac18c to your computer and use it in GitHub Desktop.
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
package com.anie.anie | |
import android.app.NotificationChannel | |
import android.app.NotificationManager | |
import android.app.PendingIntent | |
import android.content.Intent | |
import android.os.Build | |
import android.support.v4.app.NotificationCompat | |
import android.support.v4.app.NotificationCompat.DEFAULT_ALL | |
import com.anie.anie.studentHome.StudentHomeActivity | |
import com.google.firebase.messaging.FirebaseMessagingService | |
import com.google.firebase.messaging.RemoteMessage | |
class CloudMessagingService : FirebaseMessagingService() { | |
override fun onMessageReceived(message: RemoteMessage?) { | |
super.onMessageReceived(message) | |
val title = message?.data!!["title"] | |
val body = message.data["message"] | |
val fromUserId = message.data["from_user_id"] | |
val intent = Intent(applicationContext, StudentHomeActivity::class.java) | |
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0) | |
val bigText = NotificationCompat.BigTextStyle() | |
bigText.bigText(body) | |
bigText.setBigContentTitle(title) | |
bigText.setSummaryText(getString(R.string.new_message)) | |
val builder = NotificationCompat.Builder(applicationContext, getString(R.string.default_notification_channel_id)) | |
.setContentIntent(pendingIntent) | |
.setSmallIcon(R.drawable.ic_notification) | |
.setContentTitle(title) | |
.setContentText(body) | |
.setPriority(NotificationCompat.PRIORITY_MAX) | |
.setDefaults(DEFAULT_ALL) | |
.setAutoCancel(true) | |
.setStyle(bigText) | |
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
val channel = NotificationChannel(getString(R.string.default_notification_channel_id), | |
getString(R.string.default_notification_channel_name), | |
NotificationManager.IMPORTANCE_HIGH) | |
manager.createNotificationChannel(channel) | |
} | |
val chars = charArrayOf(*fromUserId?.substring(0, 3)!!.toCharArray()).map { it.toInt() }.joinToString("") | |
if (message.data["remove"] != null) | |
manager.cancel(chars.toInt()) | |
else | |
manager.notify(chars.toInt(), builder.build()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment