Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
- private static final String TAG = "MyFirebaseIIDService";
- /**
- * 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.
- */
- @Override
- public void onTokenRefresh() {
- // Get updated InstanceID token.
- String refreshedToken = FirebaseInstanceId.getInstance().getToken();
- 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) {
- DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
- reference
- .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
- .setValue(token);
- }
- }
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private static final String TAG = "MyFirebaseMsgService";
- private static final int BROADCAST_NOTIFICATION_ID = 1;
- @Override
- public void onDeletedMessages() {
- super.onDeletedMessages();
- }
- /**
- * Called when message is received.
- *
- * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
- */
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- String notificationBody = "";
- String notificationTitle = "";
- String notificationData = "";
- try{
- notificationData = remoteMessage.getData().toString();
- notificationTitle = remoteMessage.getNotification().getTitle();
- notificationBody = remoteMessage.getNotification().getBody();
- }catch (NullPointerException e){
- }
- String dataType = remoteMessage.getData().get("direct_message");
- if(dataType.equals("direct_message")){
- String title = remoteMessage.getData().get("title");
- String message = remoteMessage.getData().get("message");
- sendMessageNotification(title, message);
- }
- }
- /**
- * Build a push notification for a chat message
- * @param title
- * @param message
- */
- private void sendMessageNotification(String title, String message){
- //get the notification id
- int notificationId = (int) System.currentTimeMillis();
- // Instantiate a Builder object.
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");
- // Creates an Intent for the Activity
- Intent pendingIntent = new Intent(this, ChatActivity.class);
- // Sets the Activity to start in a new, empty task
- pendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- // Creates the PendingIntent
- PendingIntent notifyPendingIntent =
- PendingIntent.getActivity(
- this,
- 0,
- pendingIntent,
- PendingIntent.FLAG_UPDATE_CURRENT
- );
- //add properties to the builder
- builder.setSmallIcon(R.drawable.ic_notif)
- .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
- R.drawable.ic_notif))
- .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
- .setContentTitle(title)
- .setAutoCancel(true)
- //.setSubText(message)
- .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
- .setOnlyAlertOnce(true);
- builder.setContentIntent(notifyPendingIntent);
- NotificationManager mNotificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.notify(notificationId, builder.build());
- }
- }
- exports.rentPaid = functions.database.ref('/Rent_Paid_Status/{UserID}/status').onWrite((change, context) => {
- const current_status = change.after.val();
- const User_ID = context.params.UserID;
- // if(current_status.toString().trim() === "yes")
- //{
- const token_ref = admin.database().ref('/Tokens/User_ID');
- token_ref.once("value", function(data){
- //construct single value event listener.
- const token = data.val();
- console.log("token: ", token);
- //we have everythin g we need
- //Build the message payload and send the message
- console.log("Construction the notification message.");
- const payload = {
- data: {
- data_type: "direct_message",
- title: "Rent Paid",
- message: "Thank you for paying your rent!",
- }
- };
- return admin.messaging().sendToDevice(token, payload)
- .then(function(response) {
- console.log("Successfully sent message:", response);
- })
- .catch(function(error) {
- console.log("Error sending message:", error);
- });
- //send notif saying 'thank you for paying your rent!'
- });
- }
- return null;
- })
Add Comment
Please, Sign In to add comment