Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const functions = require('firebase-functions');
- const admin = require('firebase-admin');
- admin.initializeApp(functions.config().firebase);
- var rootRef =
- functions.database.ref('/notifications/{user_id}/{notification_id}');
- exports.sendNotification = rootRef.onWrite(event => {
- const user_id = event.params.user_id;
- const notification_id = event.params.notification_id;
- var request = event.data.val();
- var payload = {
- data: {
- title : "New Message from ",
- body: "userName has sent you message",
- icon: "default",
- }
- };
- console.log('We have a notification for device token: ', user_id );
- console.log('Checking if getting inside the node -- ', request.user );
- admin.messaging().sendToDevice(user_id, payload)
- .then(function(response){
- console.log('This was the notification Feature',response);
- })
- .catch(function(error){
- console.log('Error sending message ',error);
- })
- })
- if(remoteMessage.getNotification()!=null){
- Map<String,String> payload = remoteMessage.getData();
- // String title =
- remoteMessage.getNotification().getTitle();
- // String message =
- remoteMessage.getNotification().getBody();
- // Log.d(TAG, "Message Notification Title : " + title);
- // Log.d(TAG, "Message Notification Body: " + message);
- System.out.println("Messaging Service : Title - " + payload.get("title")
- + " , body - " + payload.get("body"));
- sendNotification(payload);
- }
- }
- @Override
- public void onDeletedMessages(){
- }
- private void sendNotification(Map<String,String> payload){
- NotificationCompat.Builder builder = new
- NotificationCompat.Builder(this);
- builder.setSmallIcon(R.mipmap.ic_launcher);
- builder.setContentTitle("Firebase Push
- Notification"+payload.get("title"));
- builder.setContentText("Notification Text : "+ payload.get("body"));
- Intent intent = new Intent(this, ChatWindow.class);
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
- stackBuilder.addNextIntent(intent);
- PendingIntent pendingIntent =
- stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
- builder.setContentIntent(pendingIntent);
- NotificationManager notificationManager = (NotificationManager)
- getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0, builder.build());
- }
- }
- public class FirebaseIDService extends FirebaseInstanceIdService {
- private static final String TAG = "FirebaseIDService";
- @Override
- public void onTokenRefresh() {
- // Get updated InstanceID token.
- String refreshedToken = FirebaseInstanceId.getInstance().getToken();
- Log.d(TAG, "Refreshed token: " + refreshedToken);
- DatabaseReference firebase ;//= new Firebase("https://materialtabs-
- b5734.firebaseio.com//messages");
- firebase =
- FirebaseDatabase.getInstance().getReferenceFromUrl("https://materialtabs-
- b5734.firebaseio.com//notifications/");
- // TODO: Implement this method to send any registration to your app's
- servers.
- sendRegistrationToServer(refreshedToken);
- }
- /**
- * 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) {
- // Add custom implementation, as needed.
- System.out.println("Reading Token : "+ token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement