-
Notifications
You must be signed in to change notification settings - Fork 0
Firebase Push Messaging support #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dotbugfix
wants to merge
2
commits into
AppNotification
Choose a base branch
from
FirebasePush
base: AppNotification
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.* | ||
|
|
||
|
|
@@ -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 | ||
| 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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should use toast only for failure cases?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
85 changes: 85 additions & 0 deletions
85
app/src/main/java/com/saraswitty/wittyapp/notifications/MyFirebaseMessagingService.kt
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
| 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. | ||
| } | ||
| } |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i.e. token can be null
Check if token is null here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok will do.