Skip to content

Commit 10a90bd

Browse files
committed
update get content message from firebase at onMessage because the param message return from it change structure on iOS
1 parent 3825b3b commit 10a90bd

File tree

8 files changed

+103
-29
lines changed

8 files changed

+103
-29
lines changed

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ 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+
implementation 'com.google.firebase:firebase-messaging:20.1.4'
6465
}
6566

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

android/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<uses-permission android:name="android.permission.VIBRATE" />
66

77
<application
8-
android:name="io.flutter.app.FlutterApplication"
8+
android:name=".Application"
99
android:icon="@mipmap/ic_launcher"
1010
android:label="flutter_chat_demo">
1111
<meta-data
@@ -21,10 +21,15 @@
2121
<meta-data
2222
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
2323
android:value="true" />
24+
2425
<intent-filter>
2526
<action android:name="android.intent.action.MAIN" />
2627
<category android:name="android.intent.category.LAUNCHER" />
2728
</intent-filter>
29+
<intent-filter>
30+
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
31+
<category android:name="android.intent.category.DEFAULT" />
32+
</intent-filter>
2833
</activity>
2934
</application>
3035
</manifest>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.dfa.flutterchatdemo;
2+
3+
import io.flutter.app.FlutterApplication;
4+
import io.flutter.plugin.common.PluginRegistry;
5+
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
6+
import io.flutter.plugins.GeneratedPluginRegistrant;
7+
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
8+
9+
public class Application extends FlutterApplication implements PluginRegistrantCallback {
10+
@Override
11+
public void onCreate() {
12+
super.onCreate();
13+
FlutterFirebaseMessagingService.setPluginRegistrant(this);
14+
}
15+
16+
@Override
17+
public void registerWith(PluginRegistry registry) {
18+
GeneratedPluginRegistrant.registerWith(registry);
19+
}
20+
}

android/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.4.1'
9-
classpath 'com.google.gms:google-services:4.3.0'
8+
// Example existing classpath
9+
classpath 'com.android.tools.build:gradle:3.5.3'
10+
// Add the google services classpath
11+
classpath 'com.google.gms:google-services:4.3.2'
1012
}
1113
}
1214

android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

lib/main.dart

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class MainScreenState extends State<MainScreen> {
3030
MainScreenState({Key key, @required this.currentUserId});
3131

3232
final String currentUserId;
33-
final FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
34-
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
33+
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
34+
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
35+
FlutterLocalNotificationsPlugin();
3536
final GoogleSignIn googleSignIn = GoogleSignIn();
3637

3738
bool isLoading = false;
@@ -52,7 +53,9 @@ class MainScreenState extends State<MainScreen> {
5253

5354
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
5455
print('onMessage: $message');
55-
showNotification(message['notification']);
56+
Platform.isAndroid
57+
? showNotification(message['notification'])
58+
: showNotification(message['aps']['alert']);
5659
return;
5760
}, onResume: (Map<String, dynamic> message) {
5861
print('onResume: $message');
@@ -64,30 +67,38 @@ class MainScreenState extends State<MainScreen> {
6467

6568
firebaseMessaging.getToken().then((token) {
6669
print('token: $token');
67-
Firestore.instance.collection('users').document(currentUserId).updateData({'pushToken': token});
70+
Firestore.instance
71+
.collection('users')
72+
.document(currentUserId)
73+
.updateData({'pushToken': token});
6874
}).catchError((err) {
6975
Fluttertoast.showToast(msg: err.message.toString());
7076
});
7177
}
7278

7379
void configLocalNotification() {
74-
var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');
80+
var initializationSettingsAndroid =
81+
new AndroidInitializationSettings('app_icon');
7582
var initializationSettingsIOS = new IOSInitializationSettings();
76-
var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
83+
var initializationSettings = new InitializationSettings(
84+
initializationSettingsAndroid, initializationSettingsIOS);
7785
flutterLocalNotificationsPlugin.initialize(initializationSettings);
7886
}
7987

8088
void onItemMenuPress(Choice choice) {
8189
if (choice.title == 'Log out') {
8290
handleSignOut();
8391
} else {
84-
Navigator.push(context, MaterialPageRoute(builder: (context) => Settings()));
92+
Navigator.push(
93+
context, MaterialPageRoute(builder: (context) => Settings()));
8594
}
8695
}
8796

8897
void showNotification(message) async {
8998
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
90-
Platform.isAndroid ? 'com.dfa.flutterchatdemo': 'com.duytq.flutterchatdemo',
99+
Platform.isAndroid
100+
? 'com.dfa.flutterchatdemo'
101+
: 'com.duytq.flutterchatdemo',
91102
'Flutter chat demo',
92103
'your channel description',
93104
playSound: true,
@@ -96,11 +107,20 @@ class MainScreenState extends State<MainScreen> {
96107
priority: Priority.High,
97108
);
98109
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
99-
var platformChannelSpecifics =
100-
new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
101-
await flutterLocalNotificationsPlugin.show(
102-
0, message['title'].toString(), message['body'].toString(), platformChannelSpecifics,
110+
var platformChannelSpecifics = new NotificationDetails(
111+
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
112+
113+
print(message);
114+
// print(message['body'].toString());
115+
// print(json.encode(message));
116+
117+
await flutterLocalNotificationsPlugin.show(0, message['title'].toString(),
118+
message['body'].toString(), platformChannelSpecifics,
103119
payload: json.encode(message));
120+
121+
// await flutterLocalNotificationsPlugin.show(
122+
// 0, 'plain title', 'plain body', platformChannelSpecifics,
123+
// payload: 'item x');
104124
}
105125

106126
Future<bool> onBackPress() {
@@ -113,7 +133,8 @@ class MainScreenState extends State<MainScreen> {
113133
context: context,
114134
builder: (BuildContext context) {
115135
return SimpleDialog(
116-
contentPadding: EdgeInsets.only(left: 0.0, right: 0.0, top: 0.0, bottom: 0.0),
136+
contentPadding:
137+
EdgeInsets.only(left: 0.0, right: 0.0, top: 0.0, bottom: 0.0),
117138
children: <Widget>[
118139
Container(
119140
color: themeColor,
@@ -132,7 +153,10 @@ class MainScreenState extends State<MainScreen> {
132153
),
133154
Text(
134155
'Exit app',
135-
style: TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold),
156+
style: TextStyle(
157+
color: Colors.white,
158+
fontSize: 18.0,
159+
fontWeight: FontWeight.bold),
136160
),
137161
Text(
138162
'Are you sure to exit app?',
@@ -156,7 +180,8 @@ class MainScreenState extends State<MainScreen> {
156180
),
157181
Text(
158182
'CANCEL',
159-
style: TextStyle(color: primaryColor, fontWeight: FontWeight.bold),
183+
style: TextStyle(
184+
color: primaryColor, fontWeight: FontWeight.bold),
160185
)
161186
],
162187
),
@@ -176,7 +201,8 @@ class MainScreenState extends State<MainScreen> {
176201
),
177202
Text(
178203
'YES',
179-
style: TextStyle(color: primaryColor, fontWeight: FontWeight.bold),
204+
style: TextStyle(
205+
color: primaryColor, fontWeight: FontWeight.bold),
180206
)
181207
],
182208
),
@@ -205,8 +231,9 @@ class MainScreenState extends State<MainScreen> {
205231
isLoading = false;
206232
});
207233

208-
Navigator.of(context)
209-
.pushAndRemoveUntil(MaterialPageRoute(builder: (context) => MyApp()), (Route<dynamic> route) => false);
234+
Navigator.of(context).pushAndRemoveUntil(
235+
MaterialPageRoute(builder: (context) => MyApp()),
236+
(Route<dynamic> route) => false);
210237
}
211238

212239
@override
@@ -262,7 +289,8 @@ class MainScreenState extends State<MainScreen> {
262289
} else {
263290
return ListView.builder(
264291
padding: EdgeInsets.all(10.0),
265-
itemBuilder: (context, index) => buildItem(context, snapshot.data.documents[index]),
292+
itemBuilder: (context, index) =>
293+
buildItem(context, snapshot.data.documents[index]),
266294
itemCount: snapshot.data.documents.length,
267295
);
268296
}
@@ -275,7 +303,9 @@ class MainScreenState extends State<MainScreen> {
275303
child: isLoading
276304
? Container(
277305
child: Center(
278-
child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(themeColor)),
306+
child: CircularProgressIndicator(
307+
valueColor:
308+
AlwaysStoppedAnimation<Color>(themeColor)),
279309
),
280310
color: Colors.white.withOpacity(0.8),
281311
)
@@ -302,7 +332,8 @@ class MainScreenState extends State<MainScreen> {
302332
placeholder: (context, url) => Container(
303333
child: CircularProgressIndicator(
304334
strokeWidth: 1.0,
305-
valueColor: AlwaysStoppedAnimation<Color>(themeColor),
335+
valueColor:
336+
AlwaysStoppedAnimation<Color>(themeColor),
306337
),
307338
width: 50.0,
308339
height: 50.0,
@@ -359,7 +390,8 @@ class MainScreenState extends State<MainScreen> {
359390
},
360391
color: greyColor2,
361392
padding: EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
362-
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
393+
shape:
394+
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
363395
),
364396
margin: EdgeInsets.only(bottom: 10.0, left: 5.0, right: 5.0),
365397
);

pubspec.lock

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ packages:
124124
name: flutter_local_notifications
125125
url: "https://pub.dartlang.org"
126126
source: hosted
127-
version: "0.8.4"
127+
version: "1.4.0"
128+
flutter_local_notifications_platform_interface:
129+
dependency: transitive
130+
description:
131+
name: flutter_local_notifications_platform_interface
132+
url: "https://pub.dartlang.org"
133+
source: hosted
134+
version: "1.0.1"
128135
flutter_test:
129136
dependency: "direct dev"
130137
description: flutter
@@ -235,6 +242,13 @@ packages:
235242
url: "https://pub.dartlang.org"
236243
source: hosted
237244
version: "2.2.1"
245+
plugin_platform_interface:
246+
dependency: transitive
247+
description:
248+
name: plugin_platform_interface
249+
url: "https://pub.dartlang.org"
250+
source: hosted
251+
version: "1.0.2"
238252
quiver:
239253
dependency: transitive
240254
description:
@@ -340,4 +354,4 @@ packages:
340354
version: "3.5.0"
341355
sdks:
342356
dart: ">=2.5.0 <3.0.0"
343-
flutter: ">=1.10.15-pre.148 <2.0.0"
357+
flutter: ">=1.12.13+hotfix.5 <2.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
cached_network_image: 2.0.0-rc.1
1717
intl: ^0.15.7
1818
firebase_messaging: 6.0.13
19-
flutter_local_notifications: ^0.8.4
19+
flutter_local_notifications: 1.4.0
2020
photo_view: ^0.5.0
2121

2222
dev_dependencies:

0 commit comments

Comments
 (0)