MongoDB Stitch Users - Google Group
MongoDB Stitch Announcements - Google Group
- Go to https://stitch.mongodb.com/ and log in
- Create a new app with your desired name
- Take note of the app's client App ID by going to Clients under Platform in the side pane
- Go to Authentication under Control in the side pane and enable "Allow users to log in anonymously"
-
Download and install Android Studio
-
Start a new Android Studio project
- Note: The minimum supported API level is 19 (Android 4.4 KitKat)
- Starting with an empty activity is ideal
-
In your build.gradle for your app module, add the following to your dependencies block:
compile 'org.mongodb:stitch:2.0.0'
-
Android Studio will prompt you to sync your changes in your project; hit Sync Now
- In Android Studio, go to Tools, Android, AVD manager
- Click Create Virtual Device
- Select a device that should run your app (the default is fine)
- Select and download a recommended system image of your choice (the latest is fine)
- x86_64 images are available in the x86 tab
- Name your device and hit finish
-
To initialize our connection to Stitch, go to your MainActivity.java and within your onCreate method, add the following line and replace your-app-id with the app ID you took note of when setting up the application in Stitch:
final StitchClient _client = new StitchClient(this, "your-app-id");
-
Note: To create a StitchClient using properties, make sure to set the appId property in your stitch.properties and use the following factory method:
final StitchClient _client = StitchClient.fromProperties(this);
-
-
This will only instantiate a client but will not make any outgoing connection to Stitch
-
Since we enabled anonymous log in, let's log in with it; add the following after your new _client:
_client.getAuthProviders().addOnSuccessListener(new OnSuccessListener<AvailableAuthProviders>() { @Override public void onSuccess(final AvailableAuthProviders auth) { if (auth.hasAnonymous()) { Log.d("stitch", "logging in anonymously"); _client.logInWithProvider(new AnonymousAuthProvider()).addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull final Task<String> task) { if (task.isSuccessful()) { Log.d("stitch", "logged in anonymously as user " + task.getResult()); } else { Log.e("stitch", "failed to log in anonymously", task.getException()); } } }); } else { Log.e("stitch", "no anonymous provider"); } } });
-
Now run your app in Android Studio by going to run, Run 'app'. Use the Android Virtual Device you created previously
-
Once the app is running, open up the Android Monitor by going to View, Tool Windows, Android Monitor
-
You should see log messages with stitch as a tag showing messages like:
03-12 19:16:59.003 6175-6175/? D/stitch: logging in anonymously 03-12 19:16:59.103 6175-6175/? D/stitch: logged in anonymously as user 58c5d6ebb9ede022a3d75050
- Create a Firebase Project
- Click Add Firebase to your Android app
- Skip downloading the config file
- Skip adding the Firebase SDK
- Click the gear next to overview in your Firebase project and go to Project Settings
- Go to Cloud Messaging and take note of your Legacy server key and Sender ID
- In Stitch go to the Notifications section and enter in your API Key (legacy server key) and Sender ID
-
In order to listen in on notifications arriving from GCM, we must implement the GCMListenerService
-
Create a new class called MyGCMService in your app's package and use the following code to start with:
package your.app.package.name; import com.mongodb.stitch.android.push.gcm.GCMListenerService; public class GCMService extends GCMListenerService {}
-
The included GCMListenerService contains a method called onPushMessageReceived that can be overridden to your liking
-
Now register the service and a receiver in your AndroidManifest.xml to pick up on new messages:
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="your.app.package.name" /> </intent-filter> </receiver> <service android:name=".MyGCMService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service>
-
If you'd like to give the service a chance to process the message before sleeping, add the WAKE_LOCK permission to your manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
-
-
Once logged in, you can either create a GCM Push Provider by asking Stitch for the provider information or providing it in your stitch.properties
-
To create a GCM Push Provider from properties, simply use the provided factory method:
final PushClient pushClient = _client.getPush().forProvider(GCMPushProviderInfo.fromProperties());
- Note: This assumed you've set the push.gcm.senderId and push.gcm.service property in your stitch.properties
-
To create a GCM Push Provider by asking Stitch, you must use the getPushProviders method and ensure a GCM provider exists:
_client.getPushProviders().addOnSuccessListener(new OnSuccessListener<AvailablePushProviders>() { @Override public void onSuccess(final AvailablePushProviders availablePushProviders) { if (!availablePushProviders.hasGCM()) { return; } final PushClient pushClient = _client.getPush().forProvider(availablePushProviders.getGCM()); } });
-
To register for push notifications, use the register method:
_pushClient.register().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull final Task<Void> task) { if (!task.isSuccessful()) { Log.d(TAG, "Registration failed: " + task.getException()); return; } Log.d(TAG, "Registration completed"); } });
-
To deregister from push notifications, use the deregister method:
_pushClient.deregister().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull final Task<Void> task) { if (!task.isSuccessful()) { Log.d(TAG, "Deregistration failed: " + task.getException()); return; } Log.d(TAG, "Deregistration completed"); } });
For more examples of SDK usage, see the todo app example in the examples repository