Created
June 30, 2017 10:14
-
-
Save oussemaAr/6b27d4303d78b3562aac2f1f30ba4be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.media.RingtoneManager; | |
import android.os.IBinder; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.database.ChildEventListener; | |
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 MyService extends Service { | |
private FirebaseDatabase mDatabase; | |
private DatabaseReference mRef; | |
private FirebaseAuth mAuth; | |
private String userCin; | |
public MyService() { | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO: Return the communication channel to the service. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.e("TAG", "onStartCommand: "); | |
initFirebase(); | |
mRef.child("user").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
userCin = dataSnapshot.child("xxx").getValue().toString(); | |
mRef.child("parent").child("child").addChildEventListener(new ChildEventListener() { | |
@Override | |
public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
showNotification(); | |
} | |
@Override | |
public void onChildChanged(DataSnapshot dataSnapshot, String s) { | |
} | |
@Override | |
public void onChildRemoved(DataSnapshot dataSnapshot) { | |
} | |
@Override | |
public void onChildMoved(DataSnapshot dataSnapshot, String s) { | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
} | |
}); | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
} | |
}); | |
return super.onStartCommand(intent, flags, startId); | |
} | |
private void showNotification() { | |
Intent in = new Intent(this, MainActivity.class); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, in, PendingIntent.FLAG_UPDATE_CURRENT); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) | |
.setContentTitle("Title") | |
.setContentText("Content") | |
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logoapp)) | |
.setSmallIcon(R.mipmap.logoapp) | |
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) | |
.setTicker("Ticker") | |
.setContentIntent(pendingIntent) | |
.setOnlyAlertOnce(true); | |
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
manager.notify(1, builder.build()); | |
} | |
private void initFirebase() { | |
mDatabase = FirebaseDatabase.getInstance(); | |
mRef = mDatabase.getReference(); | |
mAuth = FirebaseAuth.getInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, this code is fired several times?