Skip to content

Commit ad61334

Browse files
committed
Add Flutter GCM package and extend fitness app to use GCM.
1 parent 1cee346 commit ad61334

File tree

10 files changed

+188
-4
lines changed

10 files changed

+188
-4
lines changed

examples/fitness/BUILD.gn

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@ sky_app("fitness") {
99
manifest = "flutter.yaml"
1010

1111
if (is_android) {
12+
import("//build/config/android/config.gni")
13+
import("//build/config/android/rules.gni")
14+
1215
apk_name = "Fitness"
1316

17+
android_library("java") {
18+
java_files = [
19+
"apk/src/org/domokit/fitness/FitnessApplication.java",
20+
]
21+
22+
deps = [
23+
"//base:base_java",
24+
"//mojo/public/java:bindings",
25+
"//mojo/public/java:system",
26+
"//sky/services/gcm:gcm_lib",
27+
"//sky/services/gcm:interfaces_java",
28+
"//sky/shell:java",
29+
]
30+
}
31+
1432
deps = [
1533
"//examples/fitness/apk:resources",
34+
":java",
1635
]
1736
} else if (is_mac) {
1837
info_plist = "mac/Info.plist"

examples/fitness/apk/AndroidManifest.xml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
99
<uses-permission android:name="android.permission.INTERNET" />
1010

11-
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.sky.shell.SkyApplication">
11+
<!-- for GCM -->
12+
<uses-permission android:name="android.permission.WAKE_LOCK" />
13+
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
14+
<!-- Supposedly this permission prevents other apps from receiving our
15+
messages, but it doesn't seem to have any effect. -->
16+
<permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE"
17+
android:protectionLevel="signature" />
18+
<uses-permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE" />
19+
<!-- end for GCM -->
20+
21+
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.fitness.FitnessApplication">
1222
<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">
1323
<intent-filter>
1424
<action android:name="android.intent.action.MAIN" />
@@ -19,5 +29,34 @@
1929
android:name="org.domokit.sky.shell.UpdateService"
2030
android:exported="false"
2131
android:process=":remote"/>
32+
33+
<!-- for GCM -->
34+
<receiver
35+
android:name="com.google.android.gms.gcm.GcmReceiver"
36+
android:exported="true"
37+
android:permission="com.google.android.c2dm.permission.SEND" >
38+
<intent-filter>
39+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
40+
<category android:name="org.domokit.sky.shell" />
41+
</intent-filter>
42+
</receiver>
43+
<service
44+
android:name="org.domokit.gcm.GcmListenerService"
45+
android:exported="false" >
46+
<intent-filter>
47+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
48+
</intent-filter>
49+
</service>
50+
<service
51+
android:name="org.domokit.gcm.InstanceIDListenerService"
52+
android:exported="false">
53+
<intent-filter>
54+
<action android:name="com.google.android.gms.iid.InstanceID"/>
55+
</intent-filter>
56+
</service>
57+
<service
58+
android:name="org.domokit.gcm.RegistrationIntentService"
59+
android:exported="false">
60+
</service>
2261
</application>
2362
</manifest>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.domokit.fitness;
6+
7+
import android.content.Context;
8+
9+
import org.chromium.mojo.system.Core;
10+
import org.chromium.mojo.system.MessagePipeHandle;
11+
import org.chromium.mojom.gcm.GcmService;
12+
import org.domokit.gcm.RegistrationIntentService;
13+
import org.domokit.sky.shell.ServiceFactory;
14+
import org.domokit.sky.shell.ServiceRegistry;
15+
import org.domokit.sky.shell.SkyApplication;
16+
17+
/**
18+
* Sky implementation of {@link android.app.Application}, managing application-level global
19+
* state and initializations.
20+
*/
21+
public class FitnessApplication extends SkyApplication {
22+
/**
23+
* Override this function to register more services.
24+
*/
25+
protected void onServiceRegistryAvailable(ServiceRegistry registry) {
26+
super.onServiceRegistryAvailable(registry);
27+
28+
registry.register(GcmService.MANAGER.getName(), new ServiceFactory() {
29+
@Override
30+
public void connectToService(Context context, Core core, MessagePipeHandle pipe) {
31+
GcmService.MANAGER.bind(
32+
new RegistrationIntentService.MojoService(context), pipe);
33+
}
34+
});
35+
}
36+
}

examples/fitness/lib/main.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ library fitness;
77
import 'package:playfair/playfair.dart' as playfair;
88
import 'package:flutter/material.dart';
99
import 'package:flutter/painting.dart';
10+
import 'package:gcm/gcm.dart' as gcm;
1011

1112
import 'user_data.dart';
1213
import 'date_utils.dart';
@@ -161,6 +162,18 @@ class FitnessAppState extends State<FitnessApp> {
161162
}
162163
}
163164

164-
void main() {
165+
initGcm() async {
166+
// Register for GCM messages using the senderId provided in the
167+
// google-services.json we received when registering our app.
168+
String token;
169+
token = await gcm.registerGcmService(
170+
"858790231562", (String from, String message) {
171+
print("onMessageReceived: $from; $message");
172+
gcm.unsubscribeTopics(token, ["global"]);
173+
});
174+
gcm.subscribeTopics(token, ["global"]);
175+
}
176+
177+
main() {
165178
runApp(new FitnessApp());
166179
}

examples/fitness/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ dependencies:
55
path: ../../packages/flutter
66
playfair:
77
path: ../../packages/playfair
8+
gcm:
9+
path: ../../packages/gcm

packages/flutter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ dependencies:
88
collection: '>=1.1.3 <2.0.0'
99
intl: '>=0.12.4+2 <0.13.0'
1010
material_design_icons: '>=0.0.3 <0.1.0'
11-
sky_engine: 0.0.75
12-
sky_services: 0.0.75
11+
sky_engine: 0.0.76
12+
sky_services: 0.0.76
1313
vector_math: '>=1.4.3 <2.0.0'
1414
quiver: '>=0.21.4 <0.22.0'
1515

packages/gcm/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.idea
3+
.packages
4+
.pub/
5+
build/
6+
packages
7+
pubspec.lock

packages/gcm/lib/gcm.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2015 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Service exposed to Flutter apps that implements a subset of the GCM API.
6+
///
7+
/// This library will probably be moved into a separate package eventually.
8+
library gcm;
9+
10+
export 'src/gcm.dart';

packages/gcm/lib/src/gcm.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015, the Flutter authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/services.dart';
8+
import 'package:sky_services/gcm/gcm.mojom.dart';
9+
10+
GcmServiceProxy _initGcmService() {
11+
GcmServiceProxy gcmService = new GcmServiceProxy.unbound();
12+
shell.connectToService(null, gcmService);
13+
return gcmService;
14+
}
15+
16+
final GcmServiceProxy _gcmService = _initGcmService();
17+
18+
typedef void GcmListenerCallback(String from, String message);
19+
class _GcmListenerImpl implements GcmListener {
20+
_GcmListenerImpl(this.callback);
21+
22+
GcmListenerCallback callback;
23+
24+
void onMessageReceived(String from, String message) {
25+
callback(from, message);
26+
}
27+
}
28+
29+
Future<String> registerGcmService(String senderId, GcmListenerCallback listenerCallback) async {
30+
GcmListenerStub listener = new GcmListenerStub.unbound()
31+
..impl = new _GcmListenerImpl(listenerCallback);
32+
GcmServiceRegisterResponseParams result =
33+
await _gcmService.ptr.register(senderId, listener);
34+
return result.token;
35+
}
36+
37+
void subscribeTopics(String token, List<String> topics) {
38+
_gcmService.ptr.subscribeTopics(token, topics);
39+
}
40+
41+
void unsubscribeTopics(String token, List<String> topics) {
42+
_gcmService.ptr.unsubscribeTopics(token, topics);
43+
}

packages/gcm/pubspec.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: gcm
2+
description: Bindings for Google Cloud Messaging API
3+
version: 0.0.1
4+
author: Flutter Authors <flutter-dev@googlegroups.com>
5+
homepage: https://github.com/flutter/flutter/tree/master/packages/playfair
6+
7+
dependencies:
8+
flutter:
9+
path: ../flutter
10+
11+
dev_dependencies:
12+
test: 0.12.6
13+
14+
environment:
15+
sdk: '>=1.12.0 <2.0.0'

0 commit comments

Comments
 (0)