Skip to content

Instantly share code, notes, and snippets.

@sivsivsree
Created August 29, 2016 15:35
Show Gist options
  • Save sivsivsree/7fdc7ea11bb2c30c2cd4674cb120f359 to your computer and use it in GitHub Desktop.
Save sivsivsree/7fdc7ea11bb2c30c2cd4674cb120f359 to your computer and use it in GitHub Desktop.
package com.sivsivsree.firetest;
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.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.Toast;
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;
import java.util.concurrent.atomic.AtomicInteger;
public class NotificationService extends Service {
private static final String TAG = "SERVICE_TAG" ;
public final AtomicInteger id;
public NotificationService() {
id = new AtomicInteger(0);
}
@Override
public void onCreate() {
super.onCreate();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
generateNotification(getApplicationContext(), value, "New Notification");
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(getApplicationContext(),"Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
private void generateNotification(Context context, String message,
String key) {
Intent viewIntent = new Intent(context, MainActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(context, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.common_ic_googleplayservices)
.setContentTitle(key)
.setContentText(message)
.setGroup(TAG)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
// Build the notification and issues it with notification manager.
notificationManager.notify(id.incrementAndGet(), notificationBuilder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment