使用react-native创建本地推送通知

时间:2017-03-30 12:53:15

标签: react-native push-notification

我正在使用react-native开发android应用程序。我想通过单击按钮创建本地推送通知。我怎样才能做到这一点?我需要导入的所有库? prasanna

2 个答案:

答案 0 :(得分:0)

您可以使用react-native-fcm插件进行本地通知和预定通知。

代码段

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

如果您遇到任何问题,请检查并告知我们。 注 - 问题评论中提供的解决方案使用react-native-push-notification插件。

答案 1 :(得分: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 
 }); 
} 

调用按钮

中的scheduleNotfication()方法