When developing an application, an external service was implemented for sending push notifications, all pushes sent must have a title, text and image, however, images appear randomly in push notifications or not. Images of different dimensions were tested and sometimes they appear on push and sometimes they don't. Any idea what can be done to help identify and fix the notifications issue?
Tests done on: android 12
Image: 520x470 => 393.8kB
Service that sends the push:
Ruby 2.7.1
Rails 6.0.3.3
command-line-args: 5.2.1
firebase-admin: 10.0.2
fork do
exec(
"node #{path} --tokens #{token} --title #{title} --message #{msg} --link '#{url}' --id #{id} --image #{img}"
)
end
The image path is sent as a string
App:
React Native v0.66.3
const admin = require("firebase-admin");
const serviceAccount = require("./app-name-c0529a8230hq.json");
const cli = require("./cli")();
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://app-name.firebaseio.com"
});
const payload = {
tokens: cli.tokens.split(','),
notification: {
body: cli.message,
title: cli.title,
},
data: {
openURL: cli.link,
},
apns: {
payload: {
aps: {
'mutable-content': 1,
},
},
fcm_options: {
image: cli.image,
},
},
android: {
notification: {
image: cli.image,
},
},
};
admin
.messaging()
.sendMulticast(payload)
.then((s) => {
console.log('Success: ', s);
}
})
.catch((e) => console.log("Error", e))
Thanks for any idea to help resolve this issue.
If image have to be downloaded locally maybe the problem is the image size, perhaps is too big for network connection. It also could be the image file extention (PNG or JPEG are allowed), or the image resolution.
In other hand, if you're working with android, it could have different behaviours depending on how you build the notification json and if the android app is background or not.
What if you try the payload adding
image: cli.image,
to notification, this way:
notification: {
body: cli.message,
title: cli.title,
image: cli.image,
},
Related
I am currently using Notifee with Firebase for local push notifications on react-native android, and on debug mode it works perfectly, with my custom sound playing normally. But after assembling a release apk, on some android devices the notification sound simply does not play after a while. I've checked the notification settings on the devices and it's normal, not set to silence and the devices aren't in "do not disturb".
Here's the code :
Channel Creation at index.js
notifee.createChannel({
id: 'service',
name: 'New Notifications',
vibration: true,
sound: 'siren',
badge:false,
vibrationPattern: [300, 500],
});
Creating the notification
const onMessageReceived = async (notification) => {
const { department_name, category_name, activity_name, description, destination} = notification.data;
const messageToBePresented = `Department: ${department_name}\nCategory: ${category_name}\nActivity: ${activity_name}\nDescription: ${description}\nBed: ${destination}`;
try {
notifee.displayNotification({
title:'New Service',
body:messageToBePresented,
data: notification.data,
android:{
channelId:'service',
style: { type: AndroidStyle.BIGTEXT, text:messageToBePresented },
pressAction: {
id: 'default',
},
sound:'siren',
}
});
}
catch (err) {
console.log(err);
}
}
firebase.messaging().onMessage(onMessageReceived);
firebase.messaging().setBackgroundMessageHandler(onMessageReceived);
Is there some Android setting that i might i've overlooked that causes notification sounds to stop playing ?
How do you use a custom channel with a local notification in Expo SDK 39 with Android?
On the topic of notifications, the Expo documentation seems to include a mishmash of instructions for both depreciated and the current version of Expo.
The documentation for LegacyNotifications mentions that a “LocalNotification” object can include configuration for “channelId”.
And, in fact, the legacy methods are what the current notification guide says to use:
Notifications.presentLocalNotificationAsync({
title: 'New Message',
body: 'Message!!!!',
android: {
channelId: 'chat-messages',
},
});
But, in multiple places, the documentation says to NOT use the legacy methods:
“This API is deprecated and will be removed in the SDK 40. Please, check the new notification module.”
“Instead of presentNotificationAsync developers are encouraged to use setNotificationHandler and scheduleNotificationAsync.”
Using the current API, I’m able to create a custom channel, ‘messages’, using “setNotificationChannelAsync”:
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('messages', {
name: 'Messages',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
The documentation gives the following simple example for using “scheduleNotificationAsync”:
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! 📬",
body: 'Here is the notification body',
data: { data: 'goes here' },
},
trigger: { seconds: 2 },
});
}
According to the documentation, the only argument that “scheduleNotificationAsync” takes is “NotificationRequestInput,” which in turn can include “NotificationContentInput”. However, I did not see any mention of channelId.
Is there a way to use a custom channelId in scheduleNotificationAsync?
Folks on the Expo forums pointed out that NotificationRequestInput includes both NotificationContentInput and NotificationTriggerInput. The documentation for the NotificationTriggerInput types includes channelId.
Expanding upon the example for scheduleNotificationAsync, the simplest use of channelId would be
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! 📬",
body: "Here is the notification body",
data: { data: "goes here" },
},
trigger: {
seconds: 2,
channelId: "messages",
},
});
}
I have a cloud function which executes this code to send the notification to the user, I am getting notification correctly but I want to navigate to a particular screen for that I have to add click action something like this.
clickAction: FLUTTER_NOTIFICATION_CLICK
I have tried to put this property in different lines of code but nothing seem to work, can someone please tell where should I put it exactly?
This is my index.js file!
const message = {
token: data['guestFcmToken'],
notification: {
title: `New message from ${data['hostName']}.`,
body: data['type'] === 'image' ? 'Photo' : data['lastMessage'],
},
data: {
showForegroundNotification: 'false',
screen: 'chat'
},
}
console.log('Sending message');
const response = await admin.messaging().send(message);
console.log(response);
You can add clickAction: 'FLUTTER_NOTIFICATION_CLICK' in the following way
message = {
token: data['guestFcmToken'],
notification: {
title: `New message from ${data['hostName']}.`,
body: data['type'] === 'image' ? 'Photo' : data['lastMessage'],
},
data: {
showForegroundNotification: 'false',
screen: 'chat'
},
android: {
notification: {
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
},
}
};
I want to wake up the device after sending FCM notification.
I was able to send notifications to android devices using admin sdk in Cloud Functions. However, I cannot wake up the device while it is locked(Only sounds come). Other apps, such as Gmai, can wake up the device. Also, it works fine on iOS.
Here are the codes in Cloud Functions.
try {
const options = {
priority: "high",
};
const payload: admin.messaging.MessagingPayload = {
notification: {
title: message.title,
body: message.body,
click_action: 'FLUTTER_NOTIFICATION_CLICK',
badge: `${message.badgeNum}`,
},
};
await admin.messaging().sendToDevice(message.fcmToken, payload, options);
} catch (e) {
console.error(e.message);
throw e;
}
I'm implementing Push Notifications on my Android Ionic 2 App with the Ionic Native FCM
When I'm receiving a notification in the foreground it works, but when I'm receiving a notification in the background and if I clicked on it, nothing happens.
app.component.ts
firebaseInit(){
//Firebase
this.fcm.subscribeToTopic('all');
this.fcm.getToken()
.then(token => {
console.log(token);
this.nativeStorage.setItem('fcm-token', token);
});
this.fcm.onNotification().subscribe(
data => {
console.log("NOTIF DATA: " + JSON.stringify(data));
if(data.wasTapped){
this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})
console.info('Received in bg')
}else{
let alert = this.alertCtrl.create({
title: data.subject,
message: "New memorandum",
buttons: [
{
text: 'Ignore',
role: 'cancel'
},
{
text: 'View',
handler: () => {
this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})
}
}
]
});
alert.present();
console.info('Received in fg')
}
});
this.fcm.onTokenRefresh()
.subscribe(token => {
console.log(token);
})
}
The if(data.wasTapped) condition doesn't go off once I clicked the notification from the system tray.
EDIT
The app opens but only in the Home Page not to the designated page that I set which is this.nav.push(MemoViewPage, {memo: {_id: data.memo_id}})
I also cannot receive notifications when the app is killed or not running.
you could use push plugin instead of FCM.
this.push.createChannel({
id: "testchannel1",
description: "My first test channel",
importance: 3
}).then(() => console.log('Channel created'));
and then you could use pushObjects to specify the needs for your notification like sound, ion etc.
const options: PushOptions = {
android: {},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
After that it is easy for you to receive notifications whether you are using the app or not
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((registration: any) => this.nativeStorage.setItem('fcm-token', token));
pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));
you could use the option of forceShow:true in the pushObject init for the app to show the notification whether you are using the app or not.
And once you clicked the notification the notification payload is received by the app with the app home page set as default.