SHARE
TWEET

Untitled

a guest Mar 26th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.bud.ohad.noticeboardeylon;
  2.  
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.job.JobParameters;
  7. import android.app.job.JobService;
  8. import android.content.Intent;
  9. import android.os.Build;
  10. import android.support.v4.app.NotificationCompat;
  11. import android.support.v4.app.NotificationManagerCompat;
  12. import android.util.Log;
  13.  
  14. import com.google.firebase.auth.FirebaseAuth;
  15. import com.google.firebase.auth.FirebaseUser;
  16. import com.google.firebase.database.DataSnapshot;
  17. import com.google.firebase.database.DatabaseError;
  18. import com.google.firebase.database.DatabaseReference;
  19. import com.google.firebase.database.FirebaseDatabase;
  20. import com.google.firebase.database.ValueEventListener;
  21.  
  22.  
  23. public class TestJobService extends JobService {
  24.     private FirebaseAuth mAuth;
  25.     @Override
  26.     public boolean onStartJob(JobParameters jobParameters) {
  27.         /**
  28.          * Job has been started an alert is about to show
  29.          */
  30.         Intent service = new Intent(getApplicationContext(), MyService.class);
  31.         getApplicationContext().startService(service); // starts the service that sends the alerts
  32.         createNotificationChannel();// creates notification channel
  33.         mAuth = FirebaseAuth.getInstance();// gets user auth
  34.         FirebaseUser currentUser = mAuth.getCurrentUser(); //gets user details
  35.         if(mAuth==null||currentUser==null){
  36.             //user is not connected
  37.             showNotification("Hurry up and connect","events await you!");
  38.         }else {
  39.             //user is connected!!
  40.             final FirebaseDatabase database = FirebaseDatabase.getInstance();
  41.             DatabaseReference ref = database.getReference();
  42.             ref.child("Users").child(currentUser.getUid()).addValueEventListener(new ValueEventListener() {
  43.                 @Override
  44.                 public void onDataChange(DataSnapshot dataSnapshot) {
  45.                     //get the user details object
  46.                     ServerUser currUser = dataSnapshot.getValue(ServerUser.class);
  47.                     Log.d("2", "Current user details: " + currUser.toString());
  48.                     int group[] = new int[1];
  49.                     group[0] = currUser.getGroup();//get user group
  50.                     final FirebaseDatabase database1 = FirebaseDatabase.getInstance();
  51.                     DatabaseReference ref1 = database.getReference();
  52.                     ref1.child("Groups").child(group[0] + "").child("Events").addListenerForSingleValueEvent(new ValueEventListener() {
  53.                         @Override
  54.                         public void onDataChange(DataSnapshot dataSnapshot) {
  55.                             //count the amount of events in the user's group
  56.                             if (dataSnapshot.getChildrenCount() == 0) {
  57.                                 //no events found
  58.                                 showNotification("You have no new events ", ":(");
  59.                             } else {
  60.                                 //some events foound events found
  61.                                 showNotification("You have " + dataSnapshot.getChildrenCount() + " events!", "Come and check them out");
  62.                             }
  63.                             //scedules the next Job
  64.                             Job.scheduleJob(getApplicationContext()); // הפעלה מחדש
  65.  
  66.                         }
  67.  
  68.                         @Override
  69.                         public void onCancelled(DatabaseError databaseError) {
  70.  
  71.                         }
  72.                     });
  73.                 }
  74.  
  75.                 @Override
  76.                 public void onCancelled(DatabaseError databaseError) {
  77.                     System.out.println("The read failed: " + databaseError.getCode());
  78.                     Log.d("2", "Failed read on TestJobServiece");
  79.                 }
  80.             });
  81.  
  82.         }
  83.         return true;
  84.     }
  85.  
  86.     @Override
  87.     public boolean onStopJob(JobParameters jobParameters) {
  88.         return true;
  89.     }
  90.  
  91.     /**
  92.      *
  93.      * @param title title of the notification
  94.      * @param text the text displayed in the notification
  95.      */
  96.     public void showNotification(String title,String text) {
  97.  
  98.         Intent intent = new Intent(this, AllEvents.class);
  99.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  100.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  101.         //Builds the notification
  102.         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, BroardFunctions.NOTIFICATION_CHANNEL_ID)
  103.                 .setContentTitle(title)//sets title
  104.                 .setSmallIcon(R.mipmap.appiconblue)//sets icon
  105.                 .setContentText(text)//sets text
  106.                 .setWhen(System.currentTimeMillis())//writes date on notification
  107.                 .setStyle(new NotificationCompat.BigTextStyle()
  108.                         .bigText(text))
  109.                 .setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).setAutoCancel(true);
  110.         PendingIntent loginIntent = PendingIntent.getActivity(this, 0,
  111.                 new Intent(this, Login.class), PendingIntent.FLAG_UPDATE_CURRENT);
  112.         mBuilder.setContentIntent(loginIntent);//overrides the contect intent to login page as opposed to all events which would not work if the user is disconnected
  113.         NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
  114.         //send notification
  115.         notificationManager.notify(1, mBuilder.build());
  116.  
  117.  
  118.     }
  119.  
  120.  
  121.     private void createNotificationChannel() {
  122.         // Create the NotificationChannel, but only on API 26+ because
  123.         // the NotificationChannel class is new and not in the support library
  124.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  125.             CharSequence name = "NAME";
  126.             String description = "description";
  127.             int importance = NotificationManager.IMPORTANCE_DEFAULT;
  128.             NotificationChannel channel = new NotificationChannel(BroardFunctions.NOTIFICATION_CHANNEL_ID, name, importance);
  129.             channel.setDescription(description);
  130.             // Register the channel with the system; you can't change the importance
  131.             // or other notification behaviors after this
  132.             NotificationManager notificationManager = getSystemService(NotificationManager.class);
  133.  
  134.             notificationManager.createNotificationChannel(channel);
  135.         }
  136.  
  137.  
  138.     }
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top