I am using react-native-push-notification library to send local notifications from my ios app. I have setup deeplink using react navigation. I have also managed the local push notification to deeplink into specific app screen on click. But this requires me to set the in the message property. This means the resulting lo shows the URL in its display. This is ugly and I would like to hide the URL. Any pointers?
Here is how i schedule local notification
PushNotification.localNotificationSchedule({
title: "main title",
subtitle: "fake sub title",
message: "example://detail/one",
bigText: "My big text that will be shown when expanded",
date: new Date(Date.now() + 10 * 1000),
allowWhileIdle: false,
});
Here is my onNotification code
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
let isClicked = notification.data.userInteraction === 1
if (isClicked) {
Linking.openURL(notification.message)
}
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
Now this results in the example://detail/one url being displayed in notification. How do i achieve this?
Not exactly a solution to the above issue. But I switched to Notifee for local push notifications. And it just worked! So much easier to install and very minimal configuration needed. Was just an intuitive I expected the notification library to be.
Related
I developed react native application with rnfirebase and notifee for sending the push notification. foreground is working properly, message is displayed only once. but the background notification is displaying twice like one is from messaging().setBackgroundMessageHandler and another one is android's default push notification. First message is from default push notification and next one is from firebase messaging. So how do I remove android's default push notification. I'm also checked that first default notification is not using the firebase messaging and notifee. It's comes from outside of react native like android's native push notification
The notifications that you are seeing are most likely one from firebase and another from Notifee.
In my project I was handling the notifications that were coming from firebase via firebase.messaging().onMessage and inside this listener I was showing a local notification using Notifee so that the notification shows in the foreground.
async showNotificationInForeground(message: FirebaseMessagingTypes.RemoteMessage) {
const { messageId, notification, data } = message
const channelId = await Notifee.createChannel({
id: messageId,
name: 'Pressable Channel',
importance: AndroidImportance.HIGH,
})
await Notifee.displayNotification({
title: notification?.title || '',
body: notification?.body || '',
data,
android: {
channelId,
importance: AndroidImportance.HIGH,
pressAction: {
id: messageId,
},
smallIcon: 'ic_stat_name',
localOnly: true,
},
})
}
However what was happening was that I was calling this showNotificationInForeground method to show the local Notifee notification on both firebase's background and messaging listeners ie: firebase.messaging().onMessage and firebase.messaging().setBackgroundMessageHandler
So what I ended up doing was only calling the showNotificationInForeground method in onMessage listener and not in setBackgroundMessageHandler, which resulted in showing the local notification in the foreground, and the firebase notification in the background.
If this is not the case for you, you are most likely registering an extra notification receiver inside your AndroidManifest.xml file which is causing the duplication
I had the same problem, where there were two notifications a) 1 from notifee (which I wanted to keep) b) from FCM (which I didnt want).
Sending a data only message from my custom server solved the issue. Below is a snippet of the serverside for sending data only message :
const admin = require("firebase-admin");
// Do other stuff like create an express server to listen and trigger sending message
// Note dont forget to to initialize the app
await admin.messaging().sendToDevice(
tokens, // ['token_1', 'token_2', ...]
{
data: {
owner: "Me",
user: "My Friend",
},
},
{
// Required for background/quit data-only messages on iOS
contentAvailable: true,
// Required for background/quit data-only messages on Android
priority: "high",
}
);
You can use other methods as well, just make sure that FCM doesnt include notification and is data only.
i am using #react-native-firebase/messaging for push notification in my application.
and for custom notification sound i use #notifee/react-native this.
my current notification code is :
import messaging from '#react-native-firebase/messaging';
import notifee from '#notifee/react-native';
await notifee.createChannel({
id: 'custom-sound',
name: 'System Sound',
sound: 'notification.mp3',
});
messaging().onMessage((remoteMessage) => {
notifee.displayNotification({
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
android: {
channelId: 'custom-sound',
},
});
});
the above code works fine when the application is open on the screen so that my custom notification sound works perfectly.
but when the app is running in the background then the notification sound is the default (system default)
so how i can set the custom notification sound in all cases.
Sound related to the channel so delete your channel (custom-sound) and recreate it. Once channel created you can not edit the android channel so if you are going to edit a channel you need to delete and recreate it.
I am trying to issue Scheduled Local Notifications in order to remind user to do something even when the user is not working with the App. I want to do it in a React-Native app.
I checked the answers provided here but those answers are using Expo CLI Not React Native CLI.
Local Schedule Notification react native
React native local notifications
Also there are some packages out there like 'react-native-notifications' and 'react-native-push-notifications' on Github but I could not get a result from them. check them here https://github.com/zo0r/react-native-push-notification and here https://github.com/wix/react-native-notifications
How should I issue a Scheduled Local Notification ?
NotificationsAndroid.localNotification({
title: "Local notification",
body: "This notification was generated by the app!",
extra: "data"
});
You can use :
import PushNotification from 'react-native-push-notification';
PushNotification.localNotificationSchedule({
message: "Notification Message",
date: new Date(Date.now() + 60 * 1000), // in 60 secs
});
You can use react-native-push-notification with specific time
let now = new Date();
now.setDate(now.getDate())
now.setHours(15);
now.setMinutes(58);
now.setMilliseconds(0);
PushNotification.localNotificationSchedule({
//... You can use all the options from localNotifications
message: "My Notification Message", // (required)
repeatType: 'day',
date: new Date(now),
allowWhileIdle: true, // (optional) set notification to work while on doze
});
Im am trying to make work react-native-fcm, with firebase cloud messaging and a react-native app.
I am focusing mainly in Android
react-native#0.48.0
react-native-fcm#10.0.2
OS: Android - Emulator - 7.0.2
When my app is in foreground, its working perfectly, I receive the data in this function
FCM.on(FCMEvent.Notification, async (notif) => {
And I can handle everything
But when the app is in background, the notification is received, but when I tap the banner, the app goes foreground, the same function is triggered, but the notif object is different, and don't contains my data.
When the app is killed, also the notification is received, but when I tap the banner, the app starts, the
FCM.getInitialNotification()
is triggered, and my notif object doen't also contain the data, the same behaviour than in background
I am testing the format of the messages using node-gcm library, of firebase web interface, but nothing works.
This is a sample of my message, but I have tryed tons of combinations:
let message = new gcm.Message({
priority: 'high',
content_available: true,
timeToLive: 3,
data: {
key1: 'message1',
key2: 'message2'
},
notification: {
title: "Hello, World",
icon: "ic_launcher",
body: "This is a notification that will be displayed if your app is in the background."
},
custom_notification: {
key1: 'message1',
key2: 'message2'
}
});
How should I proceed to send and receive data in those cases? Because is driving me crazy.
If you need more data, ask for it :)
Thanks!!
in my case, i was missing the notification's data setup when building the notification on foreground notification event firebase.notifications().onNotification
new firebase.notifications.Notification()
.setNotificationId(fcmNotification.data.notificacionId)
.setTitle(fcmNotification.title)
.setBody(fcmNotification.body)
.setData(fcmNotification.data) // <- this little bastard
so, when tapping the notification data was undefined on firebase.notifications().onNotificationOpened even when the json that comes in firebase.notifications().onNotification had data field filled
I am using react native v0.45.1.
How can I add to my application notification (no matter if the app is in the background or foreground) that the user can remove only after click an acknowledge button.
I don't want the user to swipe the notification aside without notice it.
how can it be done?
I am not sure https://github.com/wix/react-native-notifications will do what I need.
Edit
I want to have a notification that will act like:
'USB debugging connected
'Touch to disable USB debugging'
The notification can't be removed unless the user actively do something, in my case it will be 'click' on a button
after a lot of digging the solution I implemented was:
setting repeat interval for the notification.
for android:
repeatType: 'time',
repeatTime: timeSpan,
for iOS:
repeatType: 'minute',
when the user click the notification (decided its not the right approached to add actions - buttons - to notification):
PushNotification.configure({
onNotification: (notification) => {
console.log('NOTIFICATION:', notification);
const clicked = notification.userInteraction;
if (clicked) {
if (Platform.OS === 'ios') {
PushNotification.cancelLocalNotifications({ id: notification.data.id });
} else {
PushNotification.cancelLocalNotifications({ id: notification.id });
}
}
},
});
I am aware that this might not be the exact answer to this question. However,
I was looking for a similar solution earlier; how to remove notifications that hasn't been dismissed or opened by a user.
Sometimes the user might open the application / screen, without clicking on the notification. The notification will still be displayed in that case.
Using React Native with Expo, this is the solution I created for removing old notifications created using their Push Notification API.
import * as Notifications from "expo-notifications";
//...
// Remove notifications that exists for this conversation!
useEffect(() => {
Notifications.getPresentedNotificationsAsync().then(res => {
for (let k in res) {
if (
res[k].request.content.data &&
res[k].request.content.data.screen === "Conversations" &&
res[k].request.content.data.conversationId === conversationId
) {
Notifications.dismissNotificationAsync(res[k].request.identifier);
console.log("removed a notification for this conversation");
}
}
});
}, []);
By using the data sent to the Push Notification API, we can determine which notifications should be removed. in this case all notifications containing a conversationId matching the current value from the route are dismissed.