0

I am developing android app using react-native. I want to create local push notification by clicking on button. how can I do this? which all the libraries I need to import? prasanna

2

2 Answers 2

0
import PushNotification from 'react-native-push-notification';

scheduleNotfication() { 
 PushNotification.localNotificationSchedule({ 
 message: "My Notification Message", // message 
 date: new Date(Date.now() + (60 * 1000)) // your required time 
 }); 
} 

call the scheduleNotfication() method inside button

Sign up to request clarification or add additional context in comments.

Comments

0

You can use react-native-fcm plugin for local notification and scheduled notification.

Code snippet

_onLocalNotification(){
    if(Platform.OS  === "android"){
        FCM.presentLocalNotification({
            id: "UNIQ_ID_STRING",                               // (optional for instant notification)
            title: this.state.content,                     // as FCM payload
            body: this.state.content,                    // 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
            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)
        });
    }
   else{
        FCM.scheduleLocalNotification({
            fire_date: new Date().getTime() + 1000,      //RN's converter is used, accept epoch time and whatever that converter supports
            id: '1111',
            body: this.state.content,
            repeat_interval: 'day',
            count: 3,
            show_in_foreground: true
        })
    }

}

_onScheduledNotification(){
    FCM.scheduleLocalNotification({
        fire_date: new Date().getTime() + 5000,      //RN's converter is used, accept epoch time and whatever that converter supports
        id: '1111',
        body: this.state.content,
        repeat_interval: 'day',
        count: 3,
        show_in_foreground: true
    })
}

Please check and let me know if you face any issue. Note - Solution provided in question's comment uses react-native-push-notification plugin.

2 Comments

for this what are the libraries need to import? FCM need to import?
run command npm install react-native-fcm react-native-link react-native-fcm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.