Skip to content

Instantly share code, notes, and snippets.

@mrblrrd
Created October 29, 2019 13:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrblrrd/13ef2ad4db858871b2e4cec070246133 to your computer and use it in GitHub Desktop.
Save mrblrrd/13ef2ad4db858871b2e4cec070246133 to your computer and use it in GitHub Desktop.
Sample methods for logging received push notifications on Android.
package com.example.ui
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.example.util.toKeyValueString
import timber.log.Timber
class AppActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
handleIntent(intent)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent?) {
// Push notification data payload is delivered in the extras of the intent of your launcher Activity.
Timber.d("New intent: $intent\nData: ${intent?.data}\nExtras: ${intent?.extras?.toKeyValueString()}")
// ...
}
}
package com.example.util
import android.os.Bundle
fun Bundle.toKeyValueString(): String =
keySet()
.fold(StringBuilder("{\n")) { sb, key -> sb.append("\t$key: ${get(key)}\n") }
.append("}\n")
.toString()
D/PushNotificationService: Message received:
{
messageId: 0:1570354827026078%90b1c65594b1c645
messageType: null
from: 736731615502
to: null
sendTime: 1572353820021
ttl: 2419200
priority: 2
originalPriority: 2
collapseKey: com.example
notification: {
title: FCM push notification
titleLocalizationKey: null
titleLocalizationArgs: null
body: Hello, world!
bodyLocalizationKey: null
bodyLocalizationArgs: null
clickAction: null
sound: null
icon: null
color: null
tag: null
channelId: null
defaultLightSettings: false
defaultSound: false
defaultVibrateSettings: false
eventTime: null
imageUrl: null
lightSettings: null
link: null
localOnly: false
notificationCount: null
notificationPriority: null
sticky: false
ticker: null
vibrateTimings: null
visibility: null
}
"data": {
key1: value1
key2: value2
}
}
D/AppActivity: New intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14000000 pkg=com.example cmp=com.example/com.example.ui.AppActivity (has extras) }
Data: null
Extras: {
google.delivered_priority: normal
google.sent_time: 1572353828002
google.ttl: 2419200
google.original_priority: normal
from: 736731615502
key1: value1
key2: value2
google.message_id: 0:1570354828704699%90b1c65594b1c645
collapse_key: com.example
}
package com.example.system.push
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import timber.log.Timber
import java.util.*
class PushNotificationService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
Timber.d("Message received:\n${remoteMessage.toKeyValueString()}")
// ...
}
override fun onNewToken(newToken: String) {
super.onNewToken(newToken)
sendRegistrationTokenToServer(newToken)
}
private fun sendRegistrationTokenToServer(newToken: String) {
Timber.d("Token: $newToken")
// ...
}
private fun RemoteMessage?.toKeyValueString(): String {
if (this == null) return "null"
val sb = StringBuilder("{\n")
sb.append("\tmessageId: $messageId\n")
sb.append("\tmessageType: $messageType\n")
sb.append("\tfrom: $from\n")
sb.append("\tto: $to\n")
sb.append("\tsendTime: $sentTime\n")
sb.append("\tttl: $ttl\n")
sb.append("\tpriority: $priority\n")
sb.append("\toriginalPriority: $originalPriority\n")
sb.append("\tcollapseKey: $collapseKey\n")
val notification = notification
if (notification != null) {
sb.append("\tnotification: {\n")
sb.append("\t\ttitle: ${notification.title}\n")
sb.append("\t\ttitleLocalizationKey: ${notification.titleLocalizationKey}\n")
sb.append("\t\ttitleLocalizationArgs: ${Arrays.toString(notification.titleLocalizationArgs)}\n")
sb.append("\t\tbody: ${notification.body}\n")
sb.append("\t\tbodyLocalizationKey: ${notification.bodyLocalizationKey}\n")
sb.append("\t\tbodyLocalizationArgs: ${Arrays.toString(notification.bodyLocalizationArgs)}\n")
sb.append("\t\tclickAction: ${notification.clickAction}\n")
sb.append("\t\tsound: ${notification.sound}\n")
sb.append("\t\ticon: ${notification.icon}\n")
sb.append("\t\tcolor: ${notification.color}\n")
sb.append("\t\ttag: ${notification.tag}\n")
sb.append("\t\tchannelId: ${notification.channelId}\n")
sb.append("\t\tdefaultLightSettings: ${notification.defaultLightSettings}\n")
sb.append("\t\tdefaultSound: ${notification.defaultSound}\n")
sb.append("\t\tdefaultVibrateSettings: ${notification.defaultVibrateSettings}\n")
sb.append("\t\teventTime: ${notification.eventTime}\n")
sb.append("\t\timageUrl: ${notification.imageUrl}\n")
sb.append("\t\tlightSettings: ${Arrays.toString(notification.lightSettings)}\n")
sb.append("\t\tlink: ${notification.link}\n")
sb.append("\t\tlocalOnly: ${notification.localOnly}\n")
sb.append("\t\tnotificationCount: ${notification.notificationCount}\n")
sb.append("\t\tnotificationPriority: ${notification.notificationPriority}\n")
sb.append("\t\tsticky: ${notification.sticky}\n")
sb.append("\t\tticker: ${notification.ticker}\n")
sb.append("\t\tvibrateTimings: ${Arrays.toString(notification.vibrateTimings)}\n")
sb.append("\t\tvisibility: ${notification.visibility}\n")
sb.append("\t}\n")
} else {
sb.append("\tnotification: null\n")
}
sb.append("\t\"data\": {\n")
for ((key, value) in data) {
sb.append("\t\t$key: $value\n")
}
sb.append("\t}\n")
sb.append("}\n")
return sb.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment