Skip to content

Commit 5cf227f

Browse files
committed
add receive notification on Android
1 parent e2f0ef7 commit 5cf227f

File tree

6 files changed

+64
-25
lines changed

6 files changed

+64
-25
lines changed

android/app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ dependencies {
6161
testImplementation 'junit:junit:4.12'
6262
androidTestImplementation 'androidx.test:runner:1.2.0'
6363
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
64-
compile 'com.google.firebase:firebase-core:17.0.0'
6564
}
6665

6766
apply plugin: 'com.google.gms.google-services'

android/app/src/main/AndroidManifest.xml

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.dfa.flutterchatdemo">
33

4-
<!-- The INTERNET permission is required for development. Specifically,
5-
flutter needs it to communicate with the running application
6-
to allow setting breakpoints, to provide hot reload, etc.
7-
-->
8-
<uses-permission android:name="android.permission.INTERNET"/>
4+
<uses-permission android:name="android.permission.INTERNET" />
95

10-
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
11-
calls FlutterMain.startInitialization(this); in its onCreate method.
12-
In most cases you can leave this as-is, but you if you want to provide
13-
additional functionality it is fine to subclass or reimplement
14-
FlutterApplication and put your custom class here. -->
156
<application
167
android:name="io.flutter.app.FlutterApplication"
17-
android:label="flutter_chat_demo"
18-
android:icon="@mipmap/ic_launcher">
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="flutter_chat_demo">
1910
<activity
2011
android:name=".MainActivity"
21-
android:launchMode="singleTop"
22-
android:theme="@style/LaunchTheme"
2312
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
2413
android:hardwareAccelerated="true"
14+
android:launchMode="singleTop"
15+
android:theme="@style/LaunchTheme"
2516
android:windowSoftInputMode="adjustResize">
26-
<!-- This keeps the window background of the activity showing
27-
until Flutter renders its first frame. It can be removed if
28-
there is no splash screen (such as the default splash screen
29-
defined in @style/LaunchTheme). -->
3017
<meta-data
3118
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
3219
android:value="true" />
20+
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
3325
<intent-filter>
34-
<action android:name="android.intent.action.MAIN"/>
35-
<category android:name="android.intent.category.LAUNCHER"/>
26+
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
27+
<category android:name="android.intent.category.DEFAULT" />
3628
</intent-filter>
3729
</activity>
3830
</application>

lib/login.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ class LoginScreenState extends State<LoginScreen> {
9191
final List<DocumentSnapshot> documents = result.documents;
9292
if (documents.length == 0) {
9393
// Update data to server if new user
94-
Firestore.instance
95-
.collection('users')
96-
.document(firebaseUser.uid)
97-
.setData({'nickname': firebaseUser.displayName, 'photoUrl': firebaseUser.photoUrl, 'id': firebaseUser.uid});
94+
Firestore.instance.collection('users').document(firebaseUser.uid).setData({
95+
'nickname': firebaseUser.displayName,
96+
'photoUrl': firebaseUser.photoUrl,
97+
'id': firebaseUser.uid,
98+
'createdAt': DateTime.now().millisecondsSinceEpoch.toString()
99+
});
98100

99101
// Write data to local
100102
currentUser = firebaseUser;

lib/main.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import 'dart:io';
44
import 'package:cached_network_image/cached_network_image.dart';
55
import 'package:cloud_firestore/cloud_firestore.dart';
66
import 'package:firebase_auth/firebase_auth.dart';
7+
import 'package:firebase_messaging/firebase_messaging.dart';
78
import 'package:flutter/material.dart';
89
import 'package:flutter_chat_demo/chat.dart';
910
import 'package:flutter_chat_demo/const.dart';
1011
import 'package:flutter_chat_demo/login.dart';
1112
import 'package:flutter_chat_demo/settings.dart';
13+
import 'package:fluttertoast/fluttertoast.dart';
1214
import 'package:google_sign_in/google_sign_in.dart';
1315

1416
void main() => runApp(MyApp());
@@ -26,13 +28,42 @@ class MainScreenState extends State<MainScreen> {
2628
MainScreenState({Key key, @required this.currentUserId});
2729

2830
final String currentUserId;
31+
final FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
2932

3033
bool isLoading = false;
3134
List<Choice> choices = const <Choice>[
3235
const Choice(title: 'Settings', icon: Icons.settings),
3336
const Choice(title: 'Log out', icon: Icons.exit_to_app),
3437
];
3538

39+
@override
40+
void initState() {
41+
super.initState();
42+
registerNotification();
43+
}
44+
45+
void registerNotification() {
46+
firebaseMessaging.requestNotificationPermissions();
47+
48+
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
49+
print('onMessage: $message');
50+
return;
51+
}, onResume: (Map<String, dynamic> message) {
52+
print('onResume: $message');
53+
return;
54+
}, onLaunch: (Map<String, dynamic> message) {
55+
print('onLaunch: $message');
56+
return;
57+
});
58+
59+
firebaseMessaging.getToken().then((token) {
60+
print('token $token');
61+
Firestore.instance.collection('users').document(currentUserId).updateData({'pushToken': token});
62+
}).catchError((err) {
63+
Fluttertoast.showToast(msg: err.message.toString());
64+
});
65+
}
66+
3667
Future<bool> onBackPress() {
3768
openDialog();
3869
return Future.value(false);

pubspec.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ packages:
7878
url: "https://pub.dartlang.org"
7979
source: hosted
8080
version: "0.4.0+6"
81+
firebase_messaging:
82+
dependency: "direct main"
83+
description:
84+
name: firebase_messaging
85+
url: "https://pub.dartlang.org"
86+
source: hosted
87+
version: "5.1.0"
8188
firebase_storage:
8289
dependency: "direct main"
8390
description:
@@ -179,6 +186,13 @@ packages:
179186
url: "https://pub.dartlang.org"
180187
source: hosted
181188
version: "1.7.0"
189+
platform:
190+
dependency: transitive
191+
description:
192+
name: platform
193+
url: "https://pub.dartlang.org"
194+
source: hosted
195+
version: "2.2.0"
182196
quiver:
183197
dependency: transitive
184198
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
firebase_storage: 3.0.2
1616
cached_network_image: ^1.0.0
1717
intl: ^0.15.7
18+
firebase_messaging: 5.1.0
1819

1920
dev_dependencies:
2021
flutter_test:

0 commit comments

Comments
 (0)