React native with firebase (push) notifications - firebase

So I have registration/login handled on my side using Laravel. I tried adding React-native-firebase so after login I could subscribe to topic like notifications/:userId so I would get notifications only for that user as currently every user is getting push notifications.
I can't find any guide on how to make this so it will work when app is in "dead" state or minimized.
I'd appreciate any help.
Thanks

You can use the below mentioned library for firebase integration.
https://rnfirebase.io/

Firebase has the Firebase Cloud Messenger, which has some notification control and badges from notifications built into it. I hope that helps, here is a link React-Native (https://rnfirebase.io/docs/v3.1.x/messaging/reference/messaging) and Firebase Docs (https://firebase.google.com/docs/reference/js/firebase.messaging)

You have generate fcmtoken after user log in, and save it to database.
After that from server side you have to fetch the fcmtoken for that particular user and call firebase pushnotification api / service.
Notification Payload :
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
If you want to send message/notification to multiple devices minor change is
tokens: ["token1", "token2"]

Related

Send a notification to all my Android devices with REST Firebase

I want to send notifications to all the android devices within my app. Actually, I can send a notification to a single token and it looks like this:
{
"to": "dwh6BFzxAOE:APA91bFenA-tySAKuNtiJGCeULewqYOwRDwfZMe4hWGN10Gx2S3sEoArZOvn_0pQihsDOtx6upcmwBbrTT2gG-rpYmPmXOvuzqMRoZy_rM6xCPyviXIn-JAAtoEEq2S4wEtGgnOBLR41",
"notification": {
"title": "My title",
"body": "This is the message"
}
}
But I don't understand how to send this notification to all devices at once. Without subscription.
I am using flutter for the apps, and I have created a react js web service to call this API REST and POST notifications.
To achieve Push Notifications via Firebase, you need to implement FireBase Cloud Messaging to push notifications to devices.
From the docs,
An FCM implementation includes two main components for sending and receiving:
A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target, and send messages.
An iOS, Android, or web (JavaScript) client app that receives messages via the corresponding platform-specific transport service.
See the documentation for server setup, or you can also use FireBase Functions. It sounds like you want to integrate Firebase Functions with Cloud Messaging. I haven't done this before, but here is a blog post doing this for a Flutter Web project:
https://medium.com/#umeshnalinde7/flutter-cloud-messaging-with-firebase-functions-firestore-android-175904a15537

How do i send a image along with FCM-push notification for nativescript?

I use nativescript-vue and this plugin for sending push notifications:
nativescript-plugin-firebase,
How do i send an image along when i send a push notification?
In my nativescript-vue project i receive the notification like this:
firebase.init({
showNotificationsWhenInForeground: true,
onMessageReceivedCallback: async (message) => {
})
How do i show an image in the notification?
I have tried with including image: "url" and that dosnt work. I think there is way to show a image in the notification because on the firebase panel there is an option to send along a image.
The firebase notification plugin only acts as a receiver and handler of notifications in this case. If you want to send a remote notification with image, you are going to do that with your notification sender; and in this case on Firebase.
If you just want to quickly send or test a push notification with an image, you can do so by going to Firebase Console > Cloud Messaging.
Then just add your image in the respective field.
Now, in most cases in production you would want to do this progmatically on your backend instead of going to Firebase Console. You can do that by using Firebase FCM SDK.
In our case, we usually do it via API calls something like:
POST https://fcm.googleapis.com/fcm/send
BODY
{
"to": "/topics/user_{{userId}}",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
Header should include a key "Authorization" with value:
key=<YOUR_FCM_KEY_HERE>
You can get your FCM key by going to Firebase Console > Project Settings (the gear) > Cloud Messaging. You can check this doc for more details on sending.
Lastly, if you really want to handle the notification image via your app (not suggested), you can force so by using a local push notification by force handling the remote push notification and doing a local push notification with your own desired image.

Firebase sometimes get token NULL for ios devices

I'm using Ionic 3 + Cordova to develop an app. In the app, I've imported the Firebase Cloud Message library to send push notifications to android/ios users. The problem is that I've already set the certifications on Apple developer panel but sometimes the token returned by the function "get_token()" on my app is returning null only for IOS users and only sometimes. Sometimes this function returns the correctly token and then the push notifications works correctly, but sometimes not.
I've already tried remove all the certifications on the developer apple menu.
The get_token() function is in the app.component.ts file, what means that the token is generated when the user open the app.
Getting the token by the firebase server:
try {
const fcmToken = await this.fcm.getToken();
Settings.push_token = fcmToken;
}
Importing the library:
import { FCM } from '#ionic-native/fcm';
This FCM variable is passed to the fcm variable inside the constructor:
public fcm: FCM
I'm hoping that the problem is on the firebase tool to get the token, because I didn't find any solution on internet.
I had the same issue and resolved it by calling get_token() only after platform is ready, when fcm is already connected:
this.platform.ready().then(() => { // first, make sure platform is ready
this.fcm.getToken().then(token => { // save the token in the backend

APNS Push Notification Structure for Firebase Deep Links

My question is about the APNS push notification data structure that is used behind the scenes for Firebase when triggering a push notification. Specifically, it's about the deep link that Firebase uses for its dynamic URLs that opens an application in a specific view.
The structure of the dynamic URL looks something like https://your_subdomain.page.link/?link=your_deep_link&apn=package_name[&amv=minimum_version][&afl=fallback_link]
. My understanding is that Firebase must eventually send an APNS push that goes to the receiving iOS application in the end. Somewhere in the sent data structure there must be the information contained in this URL.
The default APNS data structure looks something like:
{
"aps": {
"alert": {
"body": "My message"
},
"badge": 1,
"sound": "default"
}
}
Where in this structure does the Firebase dynamic link information go?
According to APN docs it must be as peer to "aps" key.

Firebase Cloud Messaging set up but not receiving push notifications from Firebase console

I've been trying to debug Firebase push notifications for quite a long time now, and got no luck. I believe I have set up provisional profiles and APNs certificate correctly. When I do not include the method
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .unknown)}
App receives notifications in foreground when sent from Firebase Notification console because it gets printed out, but it does not receive notification in background.
If I add the above method to it, it does not receive notification in foreground and background.
Anyone knows what is wrong?
I have already enabled push notifications, remote notifications, and I have setup the APNs certificate correctly. I have also tried reinstalling the app but it does not work. I have also tried all the stackoverflow posts already regarding this issue.
The code snippet that you've provided is for manual mapping of APNS token with the device registration token. Basically, you need to explicitly add this when the swizzling is disabled for your app.
Make sure to use the right token type: FIRInstanceIDAPNSTokenTypeSandbox for the sandbox environment, or FIRInstanceIDAPNSTokenTypeProd for the production environment. If you don't set the correct type, messages are not delivered to your app.
In addition, make sure that the priority is set to High in the Advanced options section when creating a new message in the Firebase Notification console.
If you're using a data message type to send notification to the client apps via FCM, then it is an intended behavior.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
Per the Firebase documentation:
On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection.

Resources