I'm using this library.
I'm attempting to get push notifications to display in the notifications tray on my Nexus 5 (android 6.0.1). Using React Native 0.42, React Native CLI 2.0.1. I'm developing on Ubuntu 14.04.
I'm using firebase. I go into my console > notifications > send a message > specific device (which I get from remote debugging console.log, below).
I am logging notifications, as you can see in code, and they do get to my device, as I can see them in the logs.
But, I don't know how to display them in the notifications tray. Looking through the docs and searching forums, it seems they should show up by default.
componentDidMount() {
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
console.log(notif)
});
});
It seems that "custom_notification" is required to display the notification in the top tray. I added this to my payload:
"custom_notification": {
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"icon":"ic_notif",
"group": "GROUP",
"id": "id",
"show_in_foreground": true
}
So, I think the app must receive the notification, parse out the data, and add this custom_notification parameter.
How about the following in your constructor:
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
// do some component related stuff
console.log(notif);
//alert(notif.fcm.body);
FCM.presentLocalNotification({
id: "UNIQ_ID_STRING", // (optional for instant notification)
title: "My Notification Title", // as FCM payload
body: notif.fcm.body, // as FCM payload (required)
sound: "default", // as FCM payload
priority: "high", // as FCM payload
click_action: "ACTION", // as FCM payload
badge: 10, // as FCM payload IOS only, set 0 to clear badges
number: 10, // Android only
ticker: "My Notification Ticker", // Android only
auto_cancel: true, // Android only (default true)
large_icon: "ic_launcher", // Android only
icon: "ic_launcher", // as FCM payload, you can relace this with custom icon you put in mipmap
big_text: "Show when notification is expanded", // Android only
sub_text: "This is a subText", // Android only
color: "red", // Android only
vibrate: 300, // Android only default: 300, no vibration if you pass null
tag: 'some_tag', // Android only
group: "group", // Android only
picture: "https://google.png", // Android only bigPicture style
ongoing: true, // Android only
my_custom_data: 'my_custom_field_value', // extra data you want to throw
lights: true, // Android only, LED blinking (default false)
show_in_foreground: true // notification when app is in foreground (local & remote)
});
});
FCM.subscribeToTopic('test_topic');
Related
I'm working with Firebase Cloud Messaging, sending notifications from mongoDB to react-native application using react-native-push-notification.
Notification is working, but I can't play the custom notification I added in my android app in /res/raw folder.
My code:
const message = {
"to": userFinded.deviceToken,
"notification": {
"title": 'Body test',
"color": '#ff9d00',
"sound": 'my_sound', //Not working
},
};
FCM Documentation
I also tried to follow the documentation of react-native-push-notification and did this:
PushNotification.localNotification({
/* Android Only Properties */
channelId: 'fcm_fallback_notification_channel', // (required) channelId, if the channel doesn't exist, notification will not trigger.
soundName: 'my_sound.mp3', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
});
PushNotification.createChannel(
{
channelId: 'fcm_fallback_notification_channel', // (required)
channelName: 'My channel', // (required)
soundName: 'my_sound.mp3', // (optional) See `soundName` parameter of `localNotification` function
});
I tried different versions:
soundName: 'my_sound.mp3';
soundName: 'my_sound';
soundName: 'android.resource://com.xyz/raw/my_sound';
soundName: 'android.resource://com.seeed/raw/my_sound';
Sometimes the notification sound is the default one, sometimes I don't have any sound.
Any help please?
you need to send android_channel_id also
like this
const message = {
"to": userFinded.deviceToken,
"notification": {
"title": 'Body test',
"color": '#ff9d00',
"sound": 'my_sound.mp3',
"android_channel_id": "fcm_fallback_notification_channel"
},
How do I give permission to allow audio in Android app without going to settings of phone?
I am using push notification with firebase cloud message, when there is received notification but no notification sound. If I go to phone's settings and turn on notifications for app then get an audio notification
Have a try by creating a custom notification channel using the react-native-push-notification npm package.
Create an android notification channel with your requirements like sound, notification importance, etc.
For example:
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
playSound: false, // (optional) default: true
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);
and add the notification channel-id name in the firebase.json to receive notification from firebase cloud messaging.
Like:
// <projectRoot>/firebase.json
{
"react-native": {
"messaging_android_notification_channel_id": "channel-id"
}
}
Check out the official docs for more information.
https://github.com/zo0r/react-native-push-notification
I am trying to stack similar messages instead of sending separate notification for every same message.
Below is my code
const payload = {
notification: {
title: `added new report`,
body: change.after.data().report_title,
tag: "ReportLog",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
sound: "default",
status: "done",
},
};
const options = {
priority: "high",
collapseKey: "ReportLog",
};
await admin.messaging().sendToDevice(tokens, payload, options);
SO suppose i send the same message again and again , instead of stacking the messages, the old notification get replace by new
On the server side you can "remember" the last notify and don't send another. (Bad idea)
On the client you can do the same. (Also bad idea)
But the thing is, that the "remember" logic won't work if the app is the background, because the notify will be handled by the system and not by your logic in the app.
I am using firebase function and sending the notification like below
let pushPayload = {
notification: {
title: 'Doodles Notification',
body: "push testing",
sound: 'default',
badge: '1'
}
};
//push tokens need to be of customer as well as all the admins in the system. fetch admin push tokens
admin.messaging().sendToDevice(pushToken, pushPayload).then(
(resp) => {
console.log("push notification sent using test method")
return
}
).catch(
(err) => {
console.log("Error sending push notification:" + JSON.stringify(err))
return
}
)
The client side ionic app has method like below:
this.fcm.onNotification().subscribe(data => {
console.log("push message is:" + JSON.stringify(data))
alert(data.aps.alert.body)
});
on ios this all works great. however on android, the console.log prints
{"wasTapped":false}
I am ok with the above expected property but where is the data? The firebase documentation is confusing around it and I am sort of lost what is the correct payload I need to use that works with both ios and android.
Your pushPayload also needs to have a data array to pass data. I believe you also need speech marks around your keys:
"notification":{
"title":"Doodles Notification",
"body":"push testing",
"sound":"default",
"badge":"1"
},
"data":{
"Key1":"Test",
"Key2":"More data"
}
Documentation
I am trying to build an app which receives notification from the server. I have configured FCM with my backend server which sends out the notifications, and every notification thats being sent from the server, i receive it on my emulator.
What i am struggling to understand is how do i list all the received notifications and display them in a view.
I am imagining how whatsapp does, you get a notification and then on click of that notification, you'll be taken to that notification full view. I presume there's an onClick action or similar to achieve this, but what i am struggling to understand is how do i list all the notifications in a particular view?
Or is it like notifications are totally separate than what can be listed in a view?
If thats the case, perhaps i will have to make another api call to fetch all the notifications.
I am using FCM and now trying to make it work on Android.
Any help is appreciated.
Thanks,
Vikram
id: new Date().valueOf().toString(), // (optional for instant notification)
title: notif.fcm.title, // as FCM payload
body: notif.fcm.body, // as FCM payload (required)
sound: "bell.mp3", // "default" or filename
priority: "high", // as FCM payload
click_action: "com.myapp.package", // as FCM payload - this is used as category identifier on iOS.
badge: 10, // as FCM payload IOS only, set 0 to clear badges
number: 10, // Android only
ticker: "My Notification Ticker", // Android only
auto_cancel: true, // Android only (default true)
large_icon:
"https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg", // Android only
icon: "ic_launcher", // as FCM payload, you can relace this with custom icon you put in mipmap
big_text: "Show when notification is expanded", // Android only
sub_text: "This is a subText", // Android only
color: "Gray", // Android only
vibrate: 300, // Android only default: 300, no vibration if you pass 0
wake_screen: true, // Android only, wake up screen when notification arrives
group: "group", // Android only
ongoing: true, // Android only
my_custom_data: "my_custom_field_value", // extra data you want to throw
lights: true, // Android only, LED blinking (default false)
show_in_foreground: true // notification when app is in foreground (local & remote)
Above is what i use to presentLocalnotification, do you mean to say that have to use the id to display it in a view?
And how would you do it?