Last active
December 15, 2017 08:05
-
-
Save k33ptoo/8c34e0c767750e88f5db2ed55db4c783 to your computer and use it in GitHub Desktop.
Firebase android show notification on new item added using a service
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
<application> | |
<service | |
android:name=".updates.BackroundLUpdateService" | |
android:enabled="true" | |
android:exported="true"></service> | |
</application> |
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.Notification; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.IBinder; | |
import android.preference.PreferenceManager; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
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.Query; | |
import com.google.firebase.database.ValueEventListener; | |
import java.util.ArrayList; | |
public class BackroundLUpdateService extends Service { | |
SharedPreferences settingsPref; | |
public BackroundLUpdateService() { | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
settingsPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | |
sessionManager = new SessionManager(getApplicationContext()); | |
// show notification | |
final ArrayList<Notes> notes = new ArrayList<>(); | |
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("updates").child("Kenya"); | |
Query items = reference.orderByChild("timestamp").limitToLast(1); | |
items.addListenerForSingleValueEvent(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot snapshot) { | |
//note information | |
for (DataSnapshot items : snapshot.getChildren()) { | |
Updates values = items.getValue(Updates.class); | |
//show notification | |
if (values != null) { | |
showNotif(values.name, values.location, values.note); | |
} | |
} | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
} | |
}); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO: Return the communication channel to the service. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
private void showNotif(String name, String place, String note) | |
{ | |
if (settingsPref.getBoolean("notifications_new_update", true)) { | |
//push a notification | |
Intent intent = new Intent(getApplicationContext(), MapActivity.class); | |
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext(), null); | |
b.setAutoCancel(true) | |
.setDefaults(Notification.DEFAULT_ALL) | |
.setWhen(System.currentTimeMillis()) | |
.setSmallIcon(R.drawable.ic_action_notif) | |
.setContentTitle("Updates") | |
.setContentText(name + " check " + place + " :" + note) | |
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) | |
.setContentIntent(contentIntent) | |
.setContentInfo("info"); | |
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(1, b.build()); | |
} | |
} | |
} |
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
//add on create | |
Intent serviceIntent = new Intent(context, BackroundLUpdateService.class); | |
context.startService(serviceIntent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment