Add Flutter GCM package and extend fitness app to use GCM.

This commit is contained in:
Matt Perry
2016-01-11 13:45:32 -05:00
parent 24c0645d93
commit 1343a02032
10 changed files with 188 additions and 4 deletions

View File

@@ -9,10 +9,29 @@ sky_app("fitness") {
manifest = "flutter.yaml"
if (is_android) {
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
apk_name = "Fitness"
android_library("java") {
java_files = [
"apk/src/org/domokit/fitness/FitnessApplication.java",
]
deps = [
"//base:base_java",
"//mojo/public/java:bindings",
"//mojo/public/java:system",
"//sky/services/gcm:gcm_lib",
"//sky/services/gcm:interfaces_java",
"//sky/shell:java",
]
}
deps = [
"//examples/fitness/apk:resources",
":java",
]
} else if (is_mac) {
info_plist = "mac/Info.plist"

View File

@@ -8,7 +8,17 @@
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.sky.shell.SkyApplication">
<!-- for GCM -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Supposedly this permission prevents other apps from receiving our
messages, but it doesn't seem to have any effect. -->
<permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE" />
<!-- end for GCM -->
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.fitness.FitnessApplication">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize" android:hardwareAccelerated="true" android:launchMode="singleTask" android:name="org.domokit.sky.shell.SkyActivity" android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -19,5 +29,34 @@
android:name="org.domokit.sky.shell.UpdateService"
android:exported="false"
android:process=":remote"/>
<!-- for GCM -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="org.domokit.sky.shell" />
</intent-filter>
</receiver>
<service
android:name="org.domokit.gcm.GcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="org.domokit.gcm.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="org.domokit.gcm.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>

View File

@@ -0,0 +1,36 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.domokit.fitness;
import android.content.Context;
import org.chromium.mojo.system.Core;
import org.chromium.mojo.system.MessagePipeHandle;
import org.chromium.mojom.gcm.GcmService;
import org.domokit.gcm.RegistrationIntentService;
import org.domokit.sky.shell.ServiceFactory;
import org.domokit.sky.shell.ServiceRegistry;
import org.domokit.sky.shell.SkyApplication;
/**
* Sky implementation of {@link android.app.Application}, managing application-level global
* state and initializations.
*/
public class FitnessApplication extends SkyApplication {
/**
* Override this function to register more services.
*/
protected void onServiceRegistryAvailable(ServiceRegistry registry) {
super.onServiceRegistryAvailable(registry);
registry.register(GcmService.MANAGER.getName(), new ServiceFactory() {
@Override
public void connectToService(Context context, Core core, MessagePipeHandle pipe) {
GcmService.MANAGER.bind(
new RegistrationIntentService.MojoService(context), pipe);
}
});
}
}

View File

@@ -7,6 +7,7 @@ library fitness;
import 'package:playfair/playfair.dart' as playfair;
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:gcm/gcm.dart' as gcm;
import 'user_data.dart';
import 'date_utils.dart';
@@ -161,6 +162,18 @@ class FitnessAppState extends State<FitnessApp> {
}
}
void main() {
initGcm() async {
// Register for GCM messages using the senderId provided in the
// google-services.json we received when registering our app.
String token;
token = await gcm.registerGcmService(
"858790231562", (String from, String message) {
print("onMessageReceived: $from; $message");
gcm.unsubscribeTopics(token, ["global"]);
});
gcm.subscribeTopics(token, ["global"]);
}
main() {
runApp(new FitnessApp());
}

View File

@@ -5,3 +5,5 @@ dependencies:
path: ../../packages/flutter
playfair:
path: ../../packages/playfair
gcm:
path: ../../packages/gcm