Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. const functions = require('firebase-functions');
  2. const admin = require('firebase-admin');
  3. admin.initializeApp(functions.config().firebase);
  4.  
  5. var rootRef =
  6. functions.database.ref('/notifications/{user_id}/{notification_id}');
  7.  
  8. exports.sendNotification = rootRef.onWrite(event => {
  9.  
  10.  
  11. const user_id = event.params.user_id;
  12. const notification_id = event.params.notification_id;
  13.  
  14. var request = event.data.val();
  15.  
  16. var payload = {
  17. data: {
  18. title : "New Message from ",
  19. body: "userName has sent you message",
  20. icon: "default",
  21. }
  22. };
  23.  
  24. console.log('We have a notification for device token: ', user_id );
  25. console.log('Checking if getting inside the node -- ', request.user );
  26.  
  27.  
  28. admin.messaging().sendToDevice(user_id, payload)
  29. .then(function(response){
  30. console.log('This was the notification Feature',response);
  31. })
  32. .catch(function(error){
  33. console.log('Error sending message ',error);
  34.  
  35. })
  36.  
  37. })
  38.  
  39. if(remoteMessage.getNotification()!=null){
  40. Map<String,String> payload = remoteMessage.getData();
  41. // String title =
  42. remoteMessage.getNotification().getTitle();
  43. // String message =
  44. remoteMessage.getNotification().getBody();
  45. // Log.d(TAG, "Message Notification Title : " + title);
  46. // Log.d(TAG, "Message Notification Body: " + message);
  47. System.out.println("Messaging Service : Title - " + payload.get("title")
  48. + " , body - " + payload.get("body"));
  49. sendNotification(payload);
  50.  
  51. }
  52. }
  53.  
  54. @Override
  55. public void onDeletedMessages(){
  56.  
  57. }
  58.  
  59. private void sendNotification(Map<String,String> payload){
  60.  
  61. NotificationCompat.Builder builder = new
  62. NotificationCompat.Builder(this);
  63. builder.setSmallIcon(R.mipmap.ic_launcher);
  64. builder.setContentTitle("Firebase Push
  65. Notification"+payload.get("title"));
  66. builder.setContentText("Notification Text : "+ payload.get("body"));
  67. Intent intent = new Intent(this, ChatWindow.class);
  68. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  69. stackBuilder.addNextIntent(intent);
  70. PendingIntent pendingIntent =
  71. stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
  72. builder.setContentIntent(pendingIntent);
  73. NotificationManager notificationManager = (NotificationManager)
  74. getSystemService(Context.NOTIFICATION_SERVICE);
  75. notificationManager.notify(0, builder.build());
  76.  
  77. }
  78.  
  79. }
  80.  
  81. public class FirebaseIDService extends FirebaseInstanceIdService {
  82. private static final String TAG = "FirebaseIDService";
  83.  
  84. @Override
  85. public void onTokenRefresh() {
  86. // Get updated InstanceID token.
  87. String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  88. Log.d(TAG, "Refreshed token: " + refreshedToken);
  89.  
  90. DatabaseReference firebase ;//= new Firebase("https://materialtabs-
  91. b5734.firebaseio.com//messages");
  92. firebase =
  93. FirebaseDatabase.getInstance().getReferenceFromUrl("https://materialtabs-
  94. b5734.firebaseio.com//notifications/");
  95.  
  96. // TODO: Implement this method to send any registration to your app's
  97. servers.
  98. sendRegistrationToServer(refreshedToken);
  99. }
  100.  
  101. /**
  102. * Persist token to third-party servers.
  103. *
  104. * Modify this method to associate the user's FCM InstanceID token with any
  105. server-side account
  106. * maintained by your application.
  107. *
  108. * @param token The new token.
  109. */
  110. private void sendRegistrationToServer(String token) {
  111. // Add custom implementation, as needed.
  112. System.out.println("Reading Token : "+ token);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement