Skip to content

Instantly share code, notes, and snippets.

@vikrum
Last active April 2, 2026 18:37
Show Gist options
  • Select an option

  • Save vikrum/6170193 to your computer and use it in GitHub Desktop.

Select an option

Save vikrum/6170193 to your computer and use it in GitHub Desktop.

Revisions

  1. Vikrum Nijjar revised this gist Aug 7, 2013. 1 changed file with 0 additions and 0 deletions.
    Binary file added Screen Shot 2013-08-06 at 5.05.57 PM.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. Vikrum Nijjar created this gist Aug 7, 2013.
    46 changes: 46 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bgfirebaseapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
    android:name="com.example.bgfirebaseapp.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <service
    android:name=".FirebaseBackgroundService"
    android:exported="false"
    android:process=":remote" >
    <intent-filter>
    <action android:name="com.example.bgfirebaseapp.FirebaseBackgroundService" />
    </intent-filter>
    </service>

    <receiver android:name=".StartFirebaseAtBoot" >
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" >
    </action>
    </intent-filter>
    </receiver>
    </application>

    </manifest>
    56 changes: 56 additions & 0 deletions FirebaseBackgroundService.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    package com.example.bgfirebaseapp;

    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 com.firebase.client.DataSnapshot;
    import com.firebase.client.Firebase;
    import com.firebase.client.ValueEventListener;

    public class FirebaseBackgroundService extends Service {

    private Firebase f = new Firebase("https://somedemo.firebaseio-demo.com/");
    private ValueEventListener handler;

    @Override
    public IBinder onBind(Intent arg0) {
    return null;
    }

    @Override
    public void onCreate() {
    super.onCreate();

    handler = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot arg0) {
    postNotif(arg0.getValue().toString());
    }

    @Override
    public void onCancelled() {
    }
    };

    f.addValueEventListener(handler);
    }

    private void postNotif(String notifString) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.ic_launcher;
    Notification notification = new Notification(icon, "Firebase" + Math.random(), System.currentTimeMillis());
    // notification.flags |= Notification.FLAG_AUTO_CANCEL;
    Context context = getApplicationContext();
    CharSequence contentTitle = "Background" + Math.random();
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, notifString, contentIntent);
    mNotificationManager.notify(1, notification);
    }

    }
    26 changes: 26 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    package com.example.bgfirebaseapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Start the background Firebase activity
    startService(new Intent(FirebaseBackgroundService.class.getName()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }
    19 changes: 19 additions & 0 deletions StartFirebaseAtBoot.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    package com.example.bgfirebaseapp;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    /**
    * Start the service when the device boots.
    *
    * @author vikrum
    *
    */
    public class StartFirebaseAtBoot extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    context.startService(new Intent(FirebaseBackgroundService.class.getName()));
    }
    }