I have firebase cloud messaging (FCM)
and can send a message to my user.
However, I want to make a scenario that if my user taps notification message on their mobile phone, then the apps will open a view or pop up and do some background task.
is this possible?
Thank You
===Update Question as suggested by Shady Boshra
1) I create google cloud function's firebase using typeScript
:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const fcm = admin.messaging();
export const sendNotif = functions.https.onRequest((request, response) => {
const tokens = request.query.tokens;
const payload: admin.messaging.MessagingPayload = {
notification: {
title: '[TEST-123] title...',
body: `[TEST-123] body of message... `,
click_action: 'FLUTTER_NOTIFICATION_CLICK',
tag: "news",
data: "{ picture: 'https://i.imgur.com/bY2bBGN.jpg', link: 'https://example.com' }"
}
};
const res = fcm.sendToDevice(tokens, payload);
response.send(res);
});
2) I update my Mobile Apps Code / Dart
:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print("::==>onMessage: $message");
message.forEach((k,v) => print("${k} - ${v}"));
String link = "https://example.com";
FlutterWebBrowser.openWebPage(url: link, androidToolbarColor: Pigment.fromString(UIData.primaryColor));
return null;
},
then i try to send push notif when apps is in opened mode.
However, when i try to debug it, i cant find the content of data. it give return below response:
I/flutter (30602): ::==>onMessage: {notification: {body: [TEST-123] body of message... , title: [TEST-123] title...}, data: {}}
I/flutter (30602): ::==>onMessage: {notification: {body: [TEST-123] body of message... , title: [TEST-123] title...}, data: {}}
I/flutter (30602): notification - {body: [TEST-123] body of message... , title: [TEST-123] title...}
I/flutter (30602): data - {}
I/flutter (30602): notification - {body: [TEST-123] body of message... , title: [TEST-123] title...}
I/flutter (30602): data - {}
as you can see, the content of data
is blank.
===Update 2
If I edit data
by removing double quotes, it return error when running firebase deploy
:
> functions@ build /Users/annixercode/myPrj/backend/firebase/functions
> tsc
src/index.ts:10:4 - error TS2322: Type '{ title: string; body: string; click_action: string; tag: string; data: { picture: string; link: string; }; }' is not assignable to type 'NotificationMessagePayload'.
Property 'data' is incompatible with index signature.
Type '{ picture: string; link: string; }' is not assignable to type 'string'.
10 notification: {
~~~~~~~~~~~~
node_modules/firebase-admin/lib/index.d.ts:4246:5
4246 notification?: admin.messaging.NotificationMessagePayload;
~~~~~~~~~~~~
The expected type comes from property 'notification' which is declared here on type 'MessagingPayload'
Found 1 error.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/anunixercode/.npm/_logs/2019-08-28T01_02_37_628Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
==Update 3 (response to Shady Boshra's answer)
below are my code on flutter:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print("::==>onMessage: $message");
message.forEach((k,v) => print("${k} - ${v}"));
var data = message['notification']['data']['link'];
print ("===>data_notif = "+data.toString());
var data2 = message['data']['link'];
print ("===>data_notif2 = "+data2.toString());
...
after i send push notif, i just got below message in debug:
The application is paused.
Reloaded 22 of 1277 libraries.
I/flutter (18608): 0
I/flutter (18608): AsyncSnapshot<String>(ConnectionState.active, Tuesday, September 3, 2019, null)
I/flutter (18608): ::==>onMessage: {notification: {body: [TEST-123] body of message... , title: [TEST-123] title...}, data: {}}
I/flutter (18608): notification - {body: [TEST-123] body of message... , title: [TEST-123] title...}
I/flutter (18608): data - {}
Application finished.
as you can see, i cant get the value of link
inside data