Skip to content
Permalink
Browse files

Sending Push Notifications Using Firebase Functions

  • Loading branch information
dragosholban committed May 13, 2018
1 parent 7241106 commit 72598ab496f3be38ec6e59652633d94aacfc2386
Binary file not shown.
@@ -30,6 +30,7 @@ dependencies {
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.google.firebase:firebase-messaging:15.0.2'
}

apply plugin: 'com.google.gms.google-services'
@@ -23,6 +23,12 @@
<activity
android:name=".FeedActivity"
android:screenOrientation="portrait"></activity>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>

</manifest>
@@ -0,0 +1,46 @@
package com.dragosholban.myinstagramapp;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

/**
* 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.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();

// 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(refreshedToken);
}
// [END refresh_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 void sendRegistrationToServer(String token) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
User user = new User(firebaseUser.getUid(), firebaseUser.getDisplayName(), token);
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("users").child(user.uid).setValue(user);
}
}
}

0 comments on commit 72598ab

Please sign in to comment.
You can’t perform that action at this time.