Advertisement
anon505

tes

Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { Image,Text, View, StatusBar, ScrollView, TouchableOpacity } from "react-native";
  3. import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';
  4.  
  5.  
  6.  
  7.  
  8. class Tes extends Component {
  9.     async componentDidMount() {
  10.         // iOS: show permission prompt for the first call. later just check permission in user settings
  11.         // Android: check permission in user settings
  12.         FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));
  13.        
  14.        
  15.        
  16.        
  17.         // 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
  18.         // 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.
  19.         // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
  20.         FCM.getInitialNotification().then(notif => {
  21.            console.log("data notif android.",notif)
  22.         });
  23.        
  24.         // 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
  25.         this.notificationListener=FCM.on(FCMEvent.Notification, async (notif) => {
  26.             // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
  27.             console.log("data notif ios",notif)
  28.            
  29.                 /*FCM.presentLocalNotification({
  30.                     id: notif.url,                               // (optional for instant notification)
  31.                     title: notif.title,                     // as FCM payload
  32.                     body: notif.body,                    // as FCM payload (required)
  33.                     sound: "default",                                   // as FCM payload
  34.                     priority: "high",                                   // as FCM payload
  35.                 //    click_action: "ACTION",                             // as FCM payload
  36.                 //    number: 10,                                         // Android only
  37.                     ticker: "My Notification Ticker",                   // Android only
  38.                   //  auto_cancel: true,                                  // Android only (default true)
  39.                    // large_icon: "ic_launcher",                           // Android only
  40.                 //    icon: "ic_launcher",                                // as FCM payload, you can relace this with custom icon you put in mipmap
  41.                  //   big_text: "Show when notification is expanded",     // Android only
  42.                   //  sub_text: "This is a subText",                      // Android only
  43.                     color: "red",                                       // Android only
  44.                     vibrate: 300,                                       // Android only default: 300, no vibration if you pass 0
  45.                   //  group: "group",                                     // Android only
  46.                    // ongoing: true,                                      // Android only
  47.                     //my_custom_data:'my_custom_field_value',             // extra data you want to throw
  48.                     lights: true,                                       // Android only, LED blinking (default false)
  49.                 });*/
  50.             if(notif.local_notification){
  51.                 return;
  52.             }
  53.             if(notif.opened_from_tray){
  54.                 return;
  55.             }
  56.            
  57.             // await someAsyncCall();
  58.  
  59.             if(Platform.OS ==='ios'){
  60.               //optional
  61.               //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.
  62.               //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
  63.               //notif._notificationType is available for iOS platfrom
  64.               switch(notif._notificationType){
  65.                 case NotificationType.Remote:
  66.                   notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
  67.                   break;
  68.                 case NotificationType.NotificationResponse:
  69.                   notif.finish();
  70.                   break;
  71.                 case NotificationType.WillPresent:
  72.                   notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
  73.                   break;
  74.               }
  75.             }
  76.         });
  77.     }
  78.  
  79.    
  80.     constructor(props) {
  81.         super(props);
  82.  
  83.         this.initialState = {
  84.             isLoading: false,
  85.         };
  86.  
  87.         this.state = this.initialState;
  88.     }
  89.  
  90.     render() {
  91.         FCM.subscribeToTopic('/topics/news');
  92.         return (
  93.                 <View>
  94.                     <Text>
  95.                         token
  96.  
  97.                     </Text>
  98.                 </View>
  99.             );
  100.     }
  101. }
  102.  
  103. export default Tes;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement