Reputation: 313
I am using react-native-push-notifications
npm library to implement notifications feature in my app. However, I want to schedule the notifications and I am using PushNotification.localNotificationSchedule
for doing so. But somehow it is not working when I am trying to trigger. I have pasted the code below. Someone could please help me with this I am new to this feature.
event Handler
const testPush = () =>{
PushNotification.localNotificationSchedule({
title:'My notification title',
date:new Date(new Date().getTime()+3000),
message:'My notification Message',
allowWhileIdle:false
});
}
Event trigger
<Pressable
onPress={testPush}
style={{ marginTop: 13 }}
>
<View style={{ backgroundColor: '#20232a', padding: 13, alignItems: 'center', borderRadius: 10 }}>
<Text style={{ color: '#ffffff' }}>Test push</Text>
</View>
</Pressable>
Upvotes: 2
Views: 12955
Reputation: 1411
adding import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
did a trick for me, try adding this into your MainApplication.java
I was able to receive localNotification but it was only scheduled notifications those were not being received.
Upvotes: 1
Reputation: 248
You have missed the small thing in the documentation ( https://github.com/zo0r/react-native-push-notification#readme ), since they have recently updated it You have to create the channel for that. Go through the documentation https://github.com/zo0r/react-native-push-notification#channel-management-android for android only and in IOS ur code works fine
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
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.);
NOTE: Without channel, notifications don't work
Add below line in the schedule code
channelId: "your-channel-id"
Add the code like below
PushNotification.localNotificationSchedule({
title:'My notification title',
date:new Date(new Date().getTime()+3000),
message:'My notification Message',
allowWhileIdle:false,
channelId: "your-channel-id"
});
Upvotes: 8
Reputation: 1940
For me your sample code worked
So Make sure you have installed, given permissions and linked correctly.
(a) Check android/build.gradle has
buildscript {
...
dependencies {
...
classpath('com.google.gms:google-services:4.3.3')
...
}
}
(b) Check In android/app/build.gradle
dependencies {
...
implementation 'com.google.firebase:firebase-analytics:17.3.0'
implementation project(':react-native-push-notification')
...
}
apply plugin: 'com.google.gms.google-services'
(c) Check In MainApplication.java
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
(d) Check In android/app/src/main/res/values/colors.xml (Create the file if it doesn't exist).
<resources>
<color name="white">#FFF</color>
</resources>
(e) Check In android/app/src/main/AndroidManifest.xml
.....
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application ....>
<!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="false"/>
<!-- Change the resource name to your App's accent color - or any other color you want -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
.....
Upvotes: 3