Skip to content

Commit 72598ab

Browse files
committed
Sending Push Notifications Using Firebase Functions
1 parent 7241106 commit 72598ab

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
implementation 'com.google.firebase:firebase-storage:15.0.0'
3131
implementation 'com.google.firebase:firebase-database:15.0.0'
3232
implementation 'com.squareup.picasso:picasso:2.71828'
33+
implementation 'com.google.firebase:firebase-messaging:15.0.2'
3334
}
3435

3536
apply plugin: 'com.google.gms.google-services'

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<activity
2424
android:name=".FeedActivity"
2525
android:screenOrientation="portrait"></activity>
26+
<service
27+
android:name=".MyFirebaseInstanceIDService">
28+
<intent-filter>
29+
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
30+
</intent-filter>
31+
</service>
2632
</application>
2733

2834
</manifest>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)