|
| 1 | +package com.dragosholban.myinstagramapp; |
| 2 | + |
| 3 | +import com.google.firebase.auth.FirebaseAuth; |
| 4 | +import com.google.firebase.auth.FirebaseUser; |
| 5 | +import com.google.firebase.database.DatabaseReference; |
| 6 | +import com.google.firebase.database.FirebaseDatabase; |
| 7 | +import com.google.firebase.iid.FirebaseInstanceId; |
| 8 | +import com.google.firebase.iid.FirebaseInstanceIdService; |
| 9 | + |
| 10 | +public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { |
| 11 | + |
| 12 | + /** |
| 13 | + * Called if InstanceID token is updated. This may occur if the security of |
| 14 | + * the previous token had been compromised. Note that this is called when the InstanceID token |
| 15 | + * is initially generated so this is where you would retrieve the token. |
| 16 | + */ |
| 17 | + // [START refresh_token] |
| 18 | + @Override |
| 19 | + public void onTokenRefresh() { |
| 20 | + // Get updated InstanceID token. |
| 21 | + String refreshedToken = FirebaseInstanceId.getInstance().getToken(); |
| 22 | + |
| 23 | + // If you want to send messages to this application instance or |
| 24 | + // manage this apps subscriptions on the server side, send the |
| 25 | + // Instance ID token to your app server. |
| 26 | + sendRegistrationToServer(refreshedToken); |
| 27 | + } |
| 28 | + // [END refresh_token] |
| 29 | + |
| 30 | + /** |
| 31 | + * Persist token to third-party servers. |
| 32 | + * |
| 33 | + * Modify this method to associate the user's FCM InstanceID token with any server-side account |
| 34 | + * maintained by your application. |
| 35 | + * |
| 36 | + * @param token The new token. |
| 37 | + */ |
| 38 | + private void sendRegistrationToServer(String token) { |
| 39 | + FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); |
| 40 | + if (firebaseUser != null) { |
| 41 | + User user = new User(firebaseUser.getUid(), firebaseUser.getDisplayName(), token); |
| 42 | + DatabaseReference database = FirebaseDatabase.getInstance().getReference(); |
| 43 | + database.child("users").child(user.uid).setValue(user); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments