Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ dependencies {
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'io.github.microutils:kotlin-logging:1.6.24'
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.21'
implementation 'com.google.firebase:firebase-messaging:17.4.0'
}

apply plugin: 'com.google.gms.google-services'
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
android:exported="true" />

<activity android:name=".NewUpcomingEvent"></activity>

<service android:name=".notifications.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

</application>

</manifest>
31 changes: 30 additions & 1 deletion app/src/main/java/com/saraswitty/wittyapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package com.saraswitty.wittyapp
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import android.widget.Toast
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessaging
import mu.KotlinLogging
import java.util.*

Expand All @@ -20,6 +23,32 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
logger.error("getInstanceId failed", task.exception)
return@OnCompleteListener
}

// Get new Instance ID token
val token = task.result?.token

// Log and toast
val msg = "[Firebase] Instance ID token: " + token
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val token = task.result?.token

i.e. token can be null
Check if token is null here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok will do.

logger.info(msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})

FirebaseMessaging.getInstance().subscribeToTopic("weather")
.addOnCompleteListener { task ->
var msg = "Subscribed to topic 'weather'"
if (!task.isSuccessful) {
msg = "Failed to subscribe to topic 'weather'"
}
logger.info("[Firebase] Subscribe to topic 'weather': " + msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should use toast only for failure cases?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This was only for quick debugging.

}

val btnLoginFacebook = findViewById<Button>(R.id.btnLoginFacebook)

// Add FB callback to login button
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.saraswitty.wittyapp.notifications

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import mu.KotlinLogging

/**
* NOTE: There can only be one service in each app that receives FCM messages. If multiple
* are declared in the Manifest then the first one will be chosen.
*
* In order to make this Kotlin sample functional, you must remove the following from the Java messaging
* service in the AndroidManifest.xml:
*
* <intent-filter>
* <action android:name="com.google.firebase.MESSAGING_EVENT" />
* </intent-filter>
*/
class MyFirebaseMessagingService : FirebaseMessagingService() {

private val logger = KotlinLogging.logger {}

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]

// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
logger.info("[Firebase] Message From: ${remoteMessage?.from}")

// Check if message contains a data payload.
remoteMessage?.data?.isNotEmpty()?.let {
logger.info("[Firebase] Message data payload: " + remoteMessage.data)
}

// Check if message contains a notification payload.
remoteMessage?.notification?.let {
logger.info("[Firebase] Message Notification Body: ${it.body}")
}

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]

// [START on_new_token]
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
override fun onNewToken(token: String?) {
logger.info("[Firebase] Refreshed token: $token")

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token)
}
// [END on_new_token]

/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private fun sendRegistrationToServer(token: String?) {
// TODO: Implement this method to send token to your app server.
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0"
classpath 'com.google.gms:google-services:4.0.1'
}
}

Expand Down