Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from "react";
- import { Image,Text, View, StatusBar, ScrollView, TouchableOpacity } from "react-native";
- import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';
- class Tes extends Component {
- async componentDidMount() {
- // iOS: show permission prompt for the first call. later just check permission in user settings
- // Android: check permission in user settings
- FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));
- // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
- // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
- // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
- FCM.getInitialNotification().then(notif => {
- console.log("data notif android.",notif)
- });
- // this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
- this.notificationListener=FCM.on(FCMEvent.Notification, async (notif) => {
- // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
- console.log("data notif ios",notif)
- /*FCM.presentLocalNotification({
- id: notif.url, // (optional for instant notification)
- title: notif.title, // as FCM payload
- body: notif.body, // as FCM payload (required)
- sound: "default", // as FCM payload
- priority: "high", // as FCM payload
- // click_action: "ACTION", // as FCM payload
- // 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 0
- // group: "group", // Android only
- // ongoing: true, // Android only
- //my_custom_data:'my_custom_field_value', // extra data you want to throw
- lights: true, // Android only, LED blinking (default false)
- });*/
- if(notif.local_notification){
- return;
- }
- if(notif.opened_from_tray){
- return;
- }
- // await someAsyncCall();
- if(Platform.OS ==='ios'){
- //optional
- //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
- //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
- //notif._notificationType is available for iOS platfrom
- switch(notif._notificationType){
- case NotificationType.Remote:
- notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
- break;
- case NotificationType.NotificationResponse:
- notif.finish();
- break;
- case NotificationType.WillPresent:
- notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
- break;
- }
- }
- });
- }
- constructor(props) {
- super(props);
- this.initialState = {
- isLoading: false,
- };
- this.state = this.initialState;
- }
- render() {
- FCM.subscribeToTopic('/topics/news');
- return (
- <View>
- <Text>
- token
- </Text>
- </View>
- );
- }
- }
- export default Tes;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement