Rnfirebase Notification not working when app is open react native - firebase

I have an app that shows notifications perfectly in the foreground and background but not working when the app is in front/open.
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state: 1111111-22333 app open in background' ,
remoteMessage.notification,
);
and
messaging().onMessage(async (remoteMessage) => {
console.log('Push Notification: laveshmahajana1 ',remoteMessage)
PushNotificationIos.addNotificationRequest({
id: remoteMessage.messageId,
body: remoteMessage.notification.body,
title: remoteMessage.notification.title,
userInfo: remoteMessage.data,
});
```

Have a try by using https://github.com/zo0r/react-native-push-notification npm.
For Example:
import PushNotification from "react-native-push-notification";
messaging().onMessage(async (remoteMessage) => {
PushNotification.localNotification({
title: remoteMessage.data.title,
message: remoteMessage.data.body,
})
})

Related

Capacitor - TypeError: Cannot read property 'getToken' of undefined

I'm trying to implement notifications with Capacitor FCM plugin in my Android app. However, when running the following code in Android Studio, I get this error message: "TypeError: Cannot read property 'getToken' of undefined".
I'm using Ionic 5 with Vue 3.
App.vue
async pushInit() {
const fcm = new FCM();
alert("...");
try {
PushNotifications.addListener("registrationError", (error) => {
console.log(`error on register ${JSON.stringify(error)}`);
}),
PushNotifications.addListener(
"pushNotificationReceived",
(notification) => {
console.log(`notification ${JSON.stringify(notification)}`);
}
),
PushNotifications.addListener(
"pushNotificationActionPerformed",
async (notification) => {
console.log("notification succeeded");
}
),
PushNotifications.register();
const fcmToken = await fcm.getToken();
alert(fcmToken);
console.log("token:" + fcmToken);
} catch (e) {
console.log(e);
}
}
I already ran npx cap sync android.
Solved it by the following steps:
Added in data return:
fcm: new FCM()
Added the following in Android Studio:
MainActivity.java: import com.getcapacitor.community.fcm.FCMPlugin; and then inside the init callback add(FCMPlugin.class);

Unable to create Dynamic Short link with React-native-firebase

I'm catching an error with code: "links/failure"
const link = new firebase.links.DynamicLink(
"LINK",
"firebaseDomainLink"
).android
.setPackageName("packagename")
.ios.setBundleId("bundleID");
firebase
.links()
.createShortDynamicLink(link, 'UNGUESSABLE')
.then(url => {
console.log(url)
})
.catch(err => {
console.warn(err);
});
However when using "createDynamicLink" I am able to get the long dynamic link as the return.
Tested with Android Platform
Picture of Yellow error box

Receiving notifications in Ionic mobile app in background and foreground through Node.js (firebase-admin)

I have an Ionic mobile application that receives notifications from the Firebase console. The application correctly receives notifications in background and foreground. However, when I send notifications from Node.js with firebase-admin plugin, only foreground notification arrives. Notifications do not arrive when the application is in the background.
What could be the problem? Does anyone know why the mobile app receive notifications in background with Firebase console but not with Node.js?
Thanks for any idea.
This is the code in the mobile application (app.component.ts):
this.fcm.subscribeToTopic('all');
platform.ready().then(() => {
this.fcm.getToken().then(token => {
console.log(token);
let alert = this.alertCtrl.create({
title: '¡new notification!',
message: token,
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('cancel button');
}
},
{
text: 'OK',
handler: () => {
console.log('ok button');
this.navCtrl.push('DetailPage');
}
}
]
});
alert.present();
});
this.fcm.onNotification().subscribe(data => {
alert('message received')
if(data.wasTapped) {
console.info("Received in background");
} else {
console.info("Received in foreground");
};
});
statusBar.styleDefault();
splashScreen.hide();
});
This is the code in Node.js:
var sys = require("util");
var admin = require('firebase-admin');
var serviceAccount = require('./xxxx.json');
var registrationToken ="xxxxxx";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xxxxx"
});
var message = {
data: {
score: '850',
time: '2:45'
},
token: registrationToken
};
admin.messaging().send(message)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error){
console.log("Error sending message", error);
});

Ionic3 Firebase notification not received on device

In brief: Firebase Notifications sent via Firebase Cloud Functions shows that message is sent. However, message is not received in the device. (Only tested in Android. Don't know about iOS)
Hello, I'm on a Ionic 3 project which uses Firebase Cloud Firestore, Cloud Functions and other Firebase services.
App workflow:
Upon new document creation (as in new reservation), the admin SDK should send push notification to the particular device which should arrive in the device.
Problem:
When checking in the Cloud Functions log, it shows message is successfully sent and the Triggering functions finished without any error. But no message has been received yet. However, when sending message from Firebase Notification Console, each message arrives perfectly.
Code:
index.ts (Cloud Functions)
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
exports.notifyOnNewBooking = functions.firestore
.document('users/{userId}/bookings/{bookingId}')
.onCreate( event => {
const bookingData = event.data.data();
// construct notification message here
const message = {
notification: {
title: 'Reservation confirmed!',
body: 'Your reservation at someplace at sometime is confirmed',
icon: 'https://image.ibb.co/iBwekx/icon.png'
}
};
// send notification with message right away
return admin.messaging().sendToDevice(bookingData.deviceFCMToken, message, {priority: 'high'})
.then(resp => {
console.log("sent successfully", resp);
})
.catch(err => {
console.error("could not send notification", err);
});
});
app.component.ts (Ionic)
...
// Ionic Native wrapper
import { FCM } from '#ionic-native/fcm';
....
#Component({
template: `
....
....
`
})
export class MyApp {
...
constructor(..., private fcm: FCM) {}
ngOnInit() {
this.fcm.onNotification()
.subscribe(resp => {});
}
}
Firebase Cloud Functions log shows this:
Ionic CLI info
cli packages: (/usr/local/lib/node_modules)
#ionic/cli-utils : 1.19.1
ionic (Ionic CLI) : 3.19.1
System:
Node : v9.3.0
npm : 5.5.1
OS : macOS High Sierra
Misc:
backend : pro
Cloud Functions package.json dependencies
"dependencies": {
"#google-cloud/functions-emulator": "^1.0.0-beta.3",
"firebase-admin": "~5.8.1",
"firebase-functions": "^0.8.1",
"firebase-tools": "^3.17.4",
"global": "^4.3.2"
},
config.xml
<plugin name="cordova-plugin-fcm" spec="^2.1.2">
<variable name="SENDER_ID" value="845539284400" />
</plugin>
Note: There is only one subscription which is at the root component in the app. And I'm on Firebase Spark plan which is free but often notifies in the log that - Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions.
Modified the function in Cloud Functions to this below and now notifications is being received in the Notification tray when the app is in Background and in the subscription response when the app is in Foreground.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
exports.notifyOnNewBooking = functions.firestore
.document('users/{userId}/bookings/{bookingId}')
.onCreate(event => {
const bookingData = event.data.data();
// construct notification message here
const message: admin.messaging.Message = {
token: bookingData.deviceFCMToken,
android: {
notification: {
title: 'Reservation successful',
body: `Your reservation at ${bookingData.restaurant_name} is confirmed.`,
icon: 'https://image.ibb.co/iBwekx/icon.png'
}
},
apns: {
headers: {
'apns-priority': '10'
},
payload: {
aps: {
alert: {
title: 'Reservation successful',
body: `Your reservation at ${bookingData.restaurant_name} is confirmed.`,
},
badge: 1
}
}
}
};
// send notification with message right away
return admin.messaging().send(message)
.then(resp => {
console.log("sent successfully", resp);
})
.catch(err => {
console.error("could not send notification", err);
});
});

Nativescript firebase vibration when app in background

When app in background, firebase plugin not calling onMassageReceived and I can't add vibration, when notification received. How I can solve that?
import firebase = require("nativescript-plugin-firebase");
var vibrator = require("nativescript-vibrate");
firebase.init({
url: AppUrl
}).then(
(instance) => {
console.log("firebase.init done true");
},
(error) => {
console.log("firebase.init error: " + error);
}
);
firebase.addOnMessageReceivedCallback((message:any)=>{
vibrator.vibration(500);
});

Resources