Guest User

Untitled

a guest
Jun 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
  2. private static final String TAG = "MyFirebaseIIDService";
  3.  
  4. /**
  5. * Called if InstanceID token is updated. This may occur if the security of
  6. * the previous token had been compromised. Note that this is called when the InstanceID token
  7. * is initially generated so this is where you would retrieve the token.
  8. */
  9. @Override
  10. public void onTokenRefresh() {
  11. // Get updated InstanceID token.
  12. String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  13. sendRegistrationToServer(refreshedToken);
  14. }
  15.  
  16.  
  17. /**
  18. * Persist token to third-party servers.
  19. *
  20. * Modify this method to associate the user's FCM InstanceID token with any server-side account
  21. * maintained by your application.
  22. *
  23. * @param token The new token.
  24. */
  25. private void sendRegistrationToServer(String token) {
  26. DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
  27. reference
  28. .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
  29. .setValue(token);
  30. }
  31. }
  32.  
  33. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  34.  
  35. private static final String TAG = "MyFirebaseMsgService";
  36. private static final int BROADCAST_NOTIFICATION_ID = 1;
  37.  
  38. @Override
  39. public void onDeletedMessages() {
  40. super.onDeletedMessages();
  41. }
  42.  
  43. /**
  44. * Called when message is received.
  45. *
  46. * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
  47. */
  48. @Override
  49. public void onMessageReceived(RemoteMessage remoteMessage) {
  50.  
  51. String notificationBody = "";
  52. String notificationTitle = "";
  53. String notificationData = "";
  54. try{
  55. notificationData = remoteMessage.getData().toString();
  56. notificationTitle = remoteMessage.getNotification().getTitle();
  57. notificationBody = remoteMessage.getNotification().getBody();
  58. }catch (NullPointerException e){
  59.  
  60. }
  61.  
  62.  
  63. String dataType = remoteMessage.getData().get("direct_message");
  64. if(dataType.equals("direct_message")){
  65. String title = remoteMessage.getData().get("title");
  66. String message = remoteMessage.getData().get("message");
  67. sendMessageNotification(title, message);
  68. }
  69. }
  70.  
  71. /**
  72. * Build a push notification for a chat message
  73. * @param title
  74. * @param message
  75. */
  76. private void sendMessageNotification(String title, String message){
  77. //get the notification id
  78.  
  79.  
  80. int notificationId = (int) System.currentTimeMillis();
  81.  
  82. // Instantiate a Builder object.
  83. NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");
  84. // Creates an Intent for the Activity
  85. Intent pendingIntent = new Intent(this, ChatActivity.class);
  86. // Sets the Activity to start in a new, empty task
  87. pendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  88. // Creates the PendingIntent
  89. PendingIntent notifyPendingIntent =
  90. PendingIntent.getActivity(
  91. this,
  92. 0,
  93. pendingIntent,
  94. PendingIntent.FLAG_UPDATE_CURRENT
  95. );
  96.  
  97. //add properties to the builder
  98. builder.setSmallIcon(R.drawable.ic_notif)
  99. .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
  100. R.drawable.ic_notif))
  101. .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  102. .setContentTitle(title)
  103. .setAutoCancel(true)
  104. //.setSubText(message)
  105. .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
  106. .setOnlyAlertOnce(true);
  107.  
  108. builder.setContentIntent(notifyPendingIntent);
  109. NotificationManager mNotificationManager =
  110. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  111.  
  112. mNotificationManager.notify(notificationId, builder.build());
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121. exports.rentPaid = functions.database.ref('/Rent_Paid_Status/{UserID}/status').onWrite((change, context) => {
  122.  
  123. const current_status = change.after.val();
  124.  
  125. const User_ID = context.params.UserID;
  126.  
  127. // if(current_status.toString().trim() === "yes")
  128. //{
  129.  
  130. const token_ref = admin.database().ref('/Tokens/User_ID');
  131. token_ref.once("value", function(data){
  132.  
  133. //construct single value event listener.
  134. const token = data.val();
  135. console.log("token: ", token);
  136.  
  137. //we have everythin g we need
  138. //Build the message payload and send the message
  139. console.log("Construction the notification message.");
  140. const payload = {
  141. data: {
  142. data_type: "direct_message",
  143. title: "Rent Paid",
  144. message: "Thank you for paying your rent!",
  145. }
  146. };
  147.  
  148. return admin.messaging().sendToDevice(token, payload)
  149. .then(function(response) {
  150. console.log("Successfully sent message:", response);
  151. })
  152. .catch(function(error) {
  153. console.log("Error sending message:", error);
  154. });
  155. //send notif saying 'thank you for paying your rent!'
  156.  
  157.  
  158. });
  159. }
  160.  
  161. return null;
  162.  
  163. })
Add Comment
Please, Sign In to add comment