I have an app which subscribes to a topic in Firebase Messaging. (for example the topic is "test").
and with cloud functions I successfully send a notification to all devices subscribed to the "test" topic.
I need when the user taps on the notification to open the app in the main screen.
this is my cloud function:
.document('testdocument/{testdc}')
.onCreate((snapshot, context) => {
return admin.messaging().sendToTopic('test', {
notification: {
title: "APP MON",
body: "Check Mon!!!",
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
},
});
});
am I missing something?
P.S: The notifications show fine (with the Icon of my app and the message I need, they just don't do anything) - also, this is needed only for ANDROID
many thanks in advance.
The correct parameter is click_action.
Related
Has anyone actually used FCM - FireBase Cloud Messaging to create device push notifications with interactive action buttons that runs when the app is is background/closed?
The goal is for the user to be able to react to the notification like WhatsApp, Messenger...
The app is cordova so anyone with experience on that would be great.
I have all the FCM setup already done and the backend working.
I already can trigger a simple notification "title+body+icon/message"
According to FCM, the payload can contain the notification section, where the OS will handle the notifications, but nothing is said about it being able to contain buttons. (So it is not that interesting).
However it's possible to use the "data" section with the values key pairs that are sent to the app to generate a local notification with the buttons, on the onMessage callback.
[UPDATE]
The push service with data does not trigger the app to create the local notification if the app was swiped (some brands kill the app when swiped from recent apps - stopping javascript execution). Conclusion: For the FCM data payload message to work, must capture the push broadcast message event (native side) with a service, and then create the notification generation (native way) with buttons based on that data payload. FCM does not support push notifications with buttons.
import { getMessaging, getToken, onMessage } from "firebase/messaging";
import { getDevice } from "framework7/lite-bundle";
function setupPushNotification(getTokenCallBack,onMessageCallBack) {
const device = getDevice();
if (device.cordova) {
FirebasePlugin.getToken((token) => getTokenCallBack(token));
FirebasePlugin.onMessageReceived((message) => {
if (message) {
onMessageCallBack && onMessageCallBack(message);
}
},function(error){
console.error(error);
});
}
}
And then to setup the whole process, creating a local notification object with buttons previously defined:
cordova.plugins.notification.local.addActionGroup('yes-no', [
{ id: 'yes', title: 'Yes', foreground: false },
{ id: 'no', title: 'No', foreground: false }
])
setupPushNotification((deviceToken)=>{
//update device token on backend database
},
(message)=>{
//This is the onMessageReceived message
//Create a local notification for exaple using the local notification cordova plugin
cordova.plugins.notification.local.schedule({
title: "Test Title",
text: "Test Body",
actionGroupId: 'yes-no',
})
})
I'm using these cordova plugins
"cordova-plugin-local-notification": "^0.9.0-beta.2",
"cordova-plugin-firebasex": "^14.2.1"
You can do almost anything with cordova, that any native app does.
Ever tried cordova-plugin-firebase-messaging? I have been able to add a chat reply text box with send button in the notifications.
you can also refer to How to add action buttons to ionic push notifications?
I am working on Cordova with react Project. I have set up google firebase google-services.json and GoogleService-Info.plist for android and ios. I am using the below package for firebase notifications setup.
https://github.com/andrehtissot/cordova-plugin-fcm-with-dependecy-updated
FCM.requestPushPermission({
ios9Support: {
timeout: 10, // How long it will wait for a decision from the user before returning `false`
interval: 0.3, // How long between each permission verification
},
}).then((wasPermissionGiven) => {
console.log(wasPermissionGiven);
alert(JSON.stringify(wasPermissionGiven));
if (wasPermissionGiven) {
FCM.getAPNSToken().then((apnsToken) => {
alert(JSON.stringify(apnsToken));
console.log(apnsToken);
});
FCM.getToken().then((apnsToken) => {
alert(JSON.stringify(apnsToken));
console.log(apnsToken);
});
FCM.subscribeToTopic('handsome');
const disposables = FCM.onNotification((payload) => {
alert(JSON.stringify(payload.toString()));
console.log('payload', payload);
});
}
});
By using the above code I am getting token and payload. Currently, I am populating the payload with a popup. can someone help me with implementing a notification instead of a popup? I searched a lot in google not found any references to implement notification with Cordova react.
Case 1: Testing app in Background State
Send the notification from firebase cloud messaging and test the APP in background state / minimise the app then you'll see the notification.
Case 2: Testing app in Foreground State
You should use some plugin something similar to this plugin https://github.com/katzer/cordova-plugin-local-notifications#readme to get the firebase notifications While app is in foreground.
I am facing an issue extremely similar to this one.
I am using Expo (SDK38) with the Managed Workflow
I am creating standalone APK builds with Turtle CLI on CI
I have an FCM project working almost perfectly with the standalone app. By almost perfectly I mean:
That I am successfully obtaining the device FCM token with the following code:
import { Notifications } from 'expo';
await Notifications.getDevicePushTokenAsync(); // Gives the token successfully
That I am sending a push notification when running the following NodeJS script, but:
const admin = require('firebase-admin');
admin.initializeApp({
credential: require('./my-credentials.json'),
databaseURL: 'https://MY_URL_HERE'
});
admin.messaging.send({
notification: { title: 'Foo', body: 'Bar' },
android: { ttl: 86400 },
token: 'THE_FCM_TOKEN_HERE'
});
[Minor issue 1] The device does not show any notification if the app is in foreground;
[Minor issue 2] The device shows the notification duplicated if the app is not in foreground.
I've mentioned the minor issues above for completeness, but the main problem I am facing now is that my app just won't notice that the notification arrived. The listener does not fire.
I tried both the Legacy Notifications Module and the New Notifications Module:
// Attempt using Legacy Notifications
// https://docs.expo.io/versions/v38.0.0/sdk/legacy-notifications/
import { Notifications as LegacyNotificationsModule } from 'expo';
// Attempt using New Notifications Module
// https://docs.expo.io/versions/v38.0.0/sdk/notifications/
import * as NewNotificationsModule from 'expo-notifications';
LegacyNotificationsModule.addListener(() => {
// Make any UI change just for we to see it happening
});
NewNotificationsModule.addNotificationReceivedListener(() => {
// Make any UI change just for we to see it happening
});
// I also tried commenting and uncommenting the code below
NewNotificationsModule.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
Recall that, like in the similar issue I linked above, I am not using Expo notification tokens (of the form ExponentPushToken[xxxxxx]). I am using standard FCM tokens obtained via Notifications.getDevicePushTokenAsync().
How can I make this work?
I finally figured it out.
I've sent a PR to Expo to improve the documentation on this.
In short, quoting my comment on GitHub:
For the app to properly react to the notification, it must come with an extra special field called experienceId. I couldn't find a way to send this field through Firebase Console interface for sending test messages; instead I made it work performing a HTTP POST call with:
Headers:
Authorization: key=${FIREBASE_SERVER_KEY_TOKEN} (obtained in my Firebase project Settings > Cloud Messaging)
Content-Type: application/json
Body:
{
"to": "<DEVICE_PUSH_NOTIFICATION_TOKEN>",
"priority": "normal",
"data": {
"experienceId": "#anonymous/my-project-slug",
"title": "Hello",
"message": "World"
}
}
A key information that wasn't documented is that, for people like me that do not have an expo account (and are building everything independently of expo), the experience ID should start with #anonymous. Full credits for #awinograd in GitHub for figuring this out.
We need to run a repeat notification every day 7 am and the content is not static it is dynamic
content needs to fetch from API
is there any way to do it in flutter for both ios and android
With out doing anything from server-side?
We used https://pub.dev/packages/flutter_local_notifications and not solved the issue?
We are developing an app for an existing website and clients have all the APIs and they are not using FCM. They will not do any changes in server-side and they highly recommend to handle it's from the app side only.
If we use Firebase messaging lib is that possible to do notification and API call in background with in the ios and android app?
Use firebase functions with pubsub. Use this code in the firebase function.
const admin = require('firebase-admin');
const fcm = admin.messaging();
exports.dailyNotification = functions.pubsub.schedule('0 7 * * *')
.onRun((context) => {
//tokens need to be unique to send notifications per device
//you can call data from firebase firestore to change the notification contents
const payload = admin.messaging.MessagingPayload = {
notification: {
title: 'Notification title',
body: 'This is a notification message',
icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
/*this token you need to save from flutter. You need it only when you
need to send specific notification to the specific device*/
fcm.sendToDevice(tokens.token, payload);
});
Use this package in flutter
Firebase messaging lib
Learn it from here that how to use firebase messaging notification.
Flutter firebase notification tutorial
Let me know if you have any questions.
I am developing a meteor app with version meteor version 1.5. I added the raix:push package and its sending notfications as desired. But if more than one notification is sent, the new notification overlaps on the previous one. Whereas I want it to be displayed as an inbox.
I tried using this :
`Push.send({
from: 'appName',
title: 'New notification',
text: 'test',
badge: 12,
gcm: {
style: 'inbox',
summaryText: 'There are many notifications'
},
query: {
userId: user.userId,
}
});`
But it doesn't work. Notifications are sent as per before only.
On checking again, I saw that nothing is being entered in the gcm field of notification collection.
Another thing I noticed is that query is stored as a string whereas gcm is an object. Does that have an affect?
Also, I would like to route to a specific page onCLick on the notification. How to add that?