Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bud.ohad.noticeboardeylon;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.job.JobParameters;
- import android.app.job.JobService;
- import android.content.Intent;
- import android.os.Build;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.app.NotificationManagerCompat;
- import android.util.Log;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- public class TestJobService extends JobService {
- private FirebaseAuth mAuth;
- @Override
- public boolean onStartJob(JobParameters jobParameters) {
- /**
- * Job has been started an alert is about to show
- */
- Intent service = new Intent(getApplicationContext(), MyService.class);
- getApplicationContext().startService(service); // starts the service that sends the alerts
- createNotificationChannel();// creates notification channel
- mAuth = FirebaseAuth.getInstance();// gets user auth
- FirebaseUser currentUser = mAuth.getCurrentUser(); //gets user details
- if(mAuth==null||currentUser==null){
- //user is not connected
- showNotification("Hurry up and connect","events await you!");
- }else {
- //user is connected!!
- final FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference ref = database.getReference();
- ref.child("Users").child(currentUser.getUid()).addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- //get the user details object
- ServerUser currUser = dataSnapshot.getValue(ServerUser.class);
- Log.d("2", "Current user details: " + currUser.toString());
- int group[] = new int[1];
- group[0] = currUser.getGroup();//get user group
- final FirebaseDatabase database1 = FirebaseDatabase.getInstance();
- DatabaseReference ref1 = database.getReference();
- ref1.child("Groups").child(group[0] + "").child("Events").addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- //count the amount of events in the user's group
- if (dataSnapshot.getChildrenCount() == 0) {
- //no events found
- showNotification("You have no new events ", ":(");
- } else {
- //some events foound events found
- showNotification("You have " + dataSnapshot.getChildrenCount() + " events!", "Come and check them out");
- }
- //scedules the next Job
- Job.scheduleJob(getApplicationContext()); // הפעלה מחדש
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- System.out.println("The read failed: " + databaseError.getCode());
- Log.d("2", "Failed read on TestJobServiece");
- }
- });
- }
- return true;
- }
- @Override
- public boolean onStopJob(JobParameters jobParameters) {
- return true;
- }
- /**
- *
- * @param title title of the notification
- * @param text the text displayed in the notification
- */
- public void showNotification(String title,String text) {
- Intent intent = new Intent(this, AllEvents.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- //Builds the notification
- NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, BroardFunctions.NOTIFICATION_CHANNEL_ID)
- .setContentTitle(title)//sets title
- .setSmallIcon(R.mipmap.appiconblue)//sets icon
- .setContentText(text)//sets text
- .setWhen(System.currentTimeMillis())//writes date on notification
- .setStyle(new NotificationCompat.BigTextStyle()
- .bigText(text))
- .setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).setAutoCancel(true);
- PendingIntent loginIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, Login.class), PendingIntent.FLAG_UPDATE_CURRENT);
- mBuilder.setContentIntent(loginIntent);//overrides the contect intent to login page as opposed to all events which would not work if the user is disconnected
- NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
- //send notification
- notificationManager.notify(1, mBuilder.build());
- }
- private void createNotificationChannel() {
- // Create the NotificationChannel, but only on API 26+ because
- // the NotificationChannel class is new and not in the support library
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- CharSequence name = "NAME";
- String description = "description";
- int importance = NotificationManager.IMPORTANCE_DEFAULT;
- NotificationChannel channel = new NotificationChannel(BroardFunctions.NOTIFICATION_CHANNEL_ID, name, importance);
- channel.setDescription(description);
- // Register the channel with the system; you can't change the importance
- // or other notification behaviors after this
- NotificationManager notificationManager = getSystemService(NotificationManager.class);
- notificationManager.createNotificationChannel(channel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement