Scheduled Local Notification in React Native - react-native

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
});

Related

Message format for deeplink with react-native-push-notification

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.

WalletConnect React Native - No events fired

I'm having a hard time getting WalletConnect 1.7.7 to work on React Native. I want to integrate in a crypto Wallet to handle dapps requests. Their documentation is...lacking. I'm following the "quickstart" in their docs, but listeners never gets fired.
import WalletConnect from "#walletconnect/client";
// Create connector
const connector = new WalletConnect(
{
// Required
uri: "wc:8a5e5bdc-a0e4-47...TJRNmhWJmoxdFo6UDk2WlhaOyQ5N0U=",
// Required
clientMeta: {
description: "WalletConnect Developer App",
url: "https://walletconnect.org",
icons: ["https://walletconnect.org/walletconnect-logo.png"],
name: "WalletConnect",
},
});
connector.on("session_request", (error, payload) => {
if (error) {
throw error;
}
// Handle Session Request
});
But session_request or any other event never get's fired. As per their documents that's all I need. Is there anything else I'm missing or perhaps it's not documented?
The documentation for Wallet Connect is very incomplete and there is very little information on the web. Are you using React Native with Expo? Because there the implementation changes. I don't see any flaws in your code. Test your integration from this website https://example.walletconnect.org/.
Using connect event instead of session_request on walllet connect works for me in react native.
connector.on('connect',(error,payload)=>{
console.log('eventtt',payload)
// Alert.alert('Connected')
})

Duplicate fcm push notification in react native android backgroundhandler

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.

issue with react native local notification and play sound

I'm creating react native app on googleplay im using react native pushNotification and react-native-sound
everything is work proper till the last update when I upload bundle on googleplay notification sound play with the sound that I use in one of react navigation page old AAB work fine but after updating this issue appears
when I clear data in android , notification sound works fine without any conflict
notification config is in App.js every thing is code proper and responsive
notification
PushNotification.localNotificationSchedule({
id: notificationId,
title: lang.notificationRemaind,
message: reminder.name, // (required)
date: new Date(Date.now() + diffSeconds),
allowWhileIdle: true,
repeatType: 'week',
channelId: 'Reminder',
priority: 'max',
soundName: 'remind',
});
sound
try {
// play the file tone.mp3
SoundPlayer.playSoundFile('firework', 'mp3')
} catch (e) {
console.log(`cannot play the sound file`, e)
}
In my case, I did not use the library for notification sound.
Android:
step1: Add my_sound.mp3 file android/app/src/main/res/raw/my_sound.mp3
step2:
PushNotification.localNotificationSchedule({
title: lang.notificationRemaind,
message: reminder.name,
playSound: 'true'
soundName: 'my_sound.mp3',
});}
step3: for local notification add playSound and SoundName in channel.
PushNotification.createChannel(
{
channelId: 'Test',
channelName: 'test-channel',
soundName: 'my_sound.mp3'
}
)
IOS:Add my_sound.mp3 file in xcode as pod level.

React native firebase messaging notification sound not change in background

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.