From 468f7bcbd6c381b07f133939e2849e2edc6a4dd9 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Sun, 15 Apr 2018 23:49:13 -0400 Subject: [PATCH] local notification running --- .../android/app/build.gradle | 21 +-- .../android/app/src/main/AndroidManifest.xml | 66 ++++---- .../quickstart/fcm/MainApplication.java | 7 +- .../firebase-migration/android/build.gradle | 6 +- .../gradle/wrapper/gradle-wrapper.properties | 4 +- Examples/firebase-migration/app/App.js | 149 ++++++------------ Examples/firebase-migration/app/Listeners.js | 18 ++- Examples/firebase-migration/index.android.js | 4 + Examples/firebase-migration/index.ios.js | 5 +- .../ios/SimpleFcmClient/AppDelegate.m | 2 + 10 files changed, 126 insertions(+), 156 deletions(-) diff --git a/Examples/firebase-migration/android/app/build.gradle b/Examples/firebase-migration/android/app/build.gradle index b4cde9f..28ec242 100644 --- a/Examples/firebase-migration/android/app/build.gradle +++ b/Examples/firebase-migration/android/app/build.gradle @@ -1,5 +1,4 @@ apply plugin: "com.android.application" -apply plugin: "com.google.gms.google-services" import com.android.build.OutputFile @@ -84,13 +83,13 @@ def enableSeparateBuildPerCPUArchitecture = false def enableProguardInReleaseBuilds = false android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 27 + buildToolsVersion "27.0.3" defaultConfig { applicationId "com.google.firebase.quickstart.fcm" minSdkVersion 16 - targetSdkVersion 22 + targetSdkVersion 26 versionCode 1 versionName "1.0" ndk { @@ -131,12 +130,14 @@ dependencies { transitive = false } compile(project(':react-native-maps')) { - exclude group: 'com.google.android.gms', module: 'play-services-base' - // This resolution make compiler ignoring play-service-base's version requirement in react-native-maps - // so that it only read from react-native-fcm - // you can also lock the version in this gradle file and ignore all module declaration - // or you can use ResolutionStragety + transitive = false } + implementation "com.google.android.gms:play-services-base:12.0.1" + implementation "com.google.firebase:firebase-core:12.0.1" + implementation "com.google.firebase:firebase-messaging:12.0.1" + implementation "com.google.android.gms:play-services-maps:12.0.1" + implementation 'me.leolin:ShortcutBadger:1.1.21@aar' + compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.facebook.react:react-native:+" // From node_modules } @@ -147,3 +148,5 @@ task copyDownloadableDepsToLibs(type: Copy) { from configurations.compile into 'libs' } + +apply plugin: 'com.google.gms.google-services' diff --git a/Examples/firebase-migration/android/app/src/main/AndroidManifest.xml b/Examples/firebase-migration/android/app/src/main/AndroidManifest.xml index 869cb8f..603fc0c 100644 --- a/Examples/firebase-migration/android/app/src/main/AndroidManifest.xml +++ b/Examples/firebase-migration/android/app/src/main/AndroidManifest.xml @@ -1,7 +1,5 @@ + package="com.google.firebase.quickstart.fcm"> @@ -9,10 +7,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/Examples/firebase-migration/android/app/src/main/java/com/google/firebase/quickstart/fcm/MainApplication.java b/Examples/firebase-migration/android/app/src/main/java/com/google/firebase/quickstart/fcm/MainApplication.java index 80aa9a2..b190173 100644 --- a/Examples/firebase-migration/android/app/src/main/java/com/google/firebase/quickstart/fcm/MainApplication.java +++ b/Examples/firebase-migration/android/app/src/main/java/com/google/firebase/quickstart/fcm/MainApplication.java @@ -5,6 +5,9 @@ import android.util.Log; import com.facebook.react.ReactApplication; import io.invertase.firebase.RNFirebasePackage; +import io.invertase.firebase.messaging.RNFirebaseMessagingPackage; +import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage; + import com.airbnb.android.react.maps.MapsPackage; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactNativeHost; @@ -27,7 +30,9 @@ public class MainApplication extends Application implements ReactApplication { return Arrays.asList( new MainReactPackage(), new RNFirebasePackage(), - new MapsPackage() + new MapsPackage(), + new RNFirebaseNotificationsPackage(), + new RNFirebaseMessagingPackage() ); } }; diff --git a/Examples/firebase-migration/android/build.gradle b/Examples/firebase-migration/android/build.gradle index 7b16caa..de858e3 100644 --- a/Examples/firebase-migration/android/build.gradle +++ b/Examples/firebase-migration/android/build.gradle @@ -3,10 +3,11 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' - classpath 'com.google.gms:google-services:3.0.0' + classpath 'com.android.tools.build:gradle:3.1.1' + classpath 'com.google.gms:google-services:3.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -21,5 +22,6 @@ allprojects { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } + google() } } diff --git a/Examples/firebase-migration/android/gradle/wrapper/gradle-wrapper.properties b/Examples/firebase-migration/android/gradle/wrapper/gradle-wrapper.properties index a27341b..bb0c361 100644 --- a/Examples/firebase-migration/android/gradle/wrapper/gradle-wrapper.properties +++ b/Examples/firebase-migration/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Jan 06 16:34:59 EST 2017 +#Sun Apr 15 21:46:36 EDT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/Examples/firebase-migration/app/App.js b/Examples/firebase-migration/app/App.js index dda70d8..d831bca 100644 --- a/Examples/firebase-migration/app/App.js +++ b/Examples/firebase-migration/app/App.js @@ -19,11 +19,9 @@ import { StackNavigator } from 'react-navigation'; import firebase from 'react-native-firebase'; -import {registerKilledListener, registerAppListener} from "./Listeners"; +import {registerAppListener} from "./Listeners"; import firebaseClient from "./FirebaseClient"; -registerKilledListener(); - class MainPage extends Component { constructor(props) { super(props); @@ -35,6 +33,13 @@ class MainPage extends Component { } async componentDidMount(){ + // Build a channel + const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max) + .setDescription('My apps test channel'); + + // Create the channel + firebase.notifications().android.createChannel(channel); + registerAppListener(this.props.navigation); firebase.notifications().getInitialNotification() .then((notificationOpen: NotificationOpen) => { @@ -55,7 +60,7 @@ class MainPage extends Component { if (!await firebase.messaging().hasPermission()) { try { await firebase.messaging().requestPermission(); - } catch { + } catch(e) { alert("Failed to grant permission") } } @@ -70,70 +75,54 @@ class MainPage extends Component { firebase.messaging().unsubscribeFromTopic('sometopic'); } - componentWillMount(){ + componentWillUnmount(){ this.onTokenRefreshListener(); this.notificationOpenedListener(); this.messageListener(); } showLocalNotification() { - if(Platform.OS === 'ios'){ - const notification = new firebase.notifications.Notification() - .setNotificationId(new Date().valueOf().toString()) - .setTitle( "Test Notification with action") - .setBody("Force touch to reply") - .setSound("bell.mp3") - .setCategory() - .setBadge(10) - .setData({ - key1: 'value1', - key2: 'value2', - }); - } - - FCM.presentLocalNotification({ - id: new Date().valueOf().toString(), // (optional for instant notification) - title: "Test Notification with action", // as FCM payload - body: "Force touch to reply", // as FCM payload (required) - sound: "bell.mp3", // "default" or filename - priority: "high", // as FCM payload - click_action: "com.myapp.MyCategory", // as FCM payload - this is used as category identifier on iOS. - 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: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg", // 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 - wake_screen: true, // Android only, wake up screen when notification arrives - group: "group", // Android only - picture: "https://google.png", // Android only bigPicture style - 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) - show_in_foreground: true // notification when app is in foreground (local & remote) + let notification = new firebase.notifications.Notification(); + notification = notification.setNotificationId(new Date().valueOf().toString()) + .setTitle( "Test Notification with action") + .setBody("Force touch to reply") + .setSound("bell.mp3") + .setData({ + key1: 'value1', + key2: 'value2' }); + notification.ios.badge = 10 + notification.android.setAutoCancel(true); + + notification.android.setBigPicture("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png", "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg", "content title", "summary text") + notification.android.setColor("red") + notification.android.setColorized(true) + notification.android.setOngoing(true) + notification.android.setPriority(firebase.notifications.Android.Priority.High) + notification.android.setSmallIcon("ic_launcher") + notification.android.setVibrate([300]) + notification.android.addAction(new firebase.notifications.Android.Action("view", "ic_launcher", "VIEW")) + notification.android.addAction(new firebase.notifications.Android.Action("dismiss", "ic_launcher", "DISMISS")) + notification.android.setChannelId("test-channel") + + firebase.notifications().displayNotification(notification) } scheduleLocalNotification() { - FCM.scheduleLocalNotification({ - id: 'testnotif', - fire_date: new Date().getTime()+5000, - vibrate: 500, - title: 'Hello', - body: 'Test Scheduled Notification', - sub_text: 'sub text', - priority: "high", - large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg", - show_in_foreground: true, - picture: 'https://firebase.google.com/_static/af7ae4b3fc/images/firebase/lockup.png', - wake_screen: true, - extra1: {a: 1}, - extra2: 1 + let notification = new firebase.notifications.Notification(); + notification = notification.setNotificationId(new Date().valueOf().toString()) + .setTitle( "Test Notification with action") + .setBody("Force touch to reply") + .setSound("bell.mp3") + .setData({ + key1: 'value1', + key2: 'value2' }); + notification.android.setChannelId("test-channel") + notification.android.setPriority(firebase.notifications.Android.Priority.High) + notification.android.setSmallIcon("ic_launcher") + + firebase.notifications().scheduleNotification(notification, { fireDate: new Date().getTime() + 5000 }) } sendRemoteNotification(token) { @@ -146,10 +135,7 @@ class MainPage extends Component { "custom_notification": { "title": "Simple FCM Client", "body": "Click me to go to detail", - "sound": "default", - "priority": "high", - "show_in_foreground": true, - targetScreen: 'detail' + data: {targetScreen: 'detail'} } }, "priority": 10 @@ -172,37 +158,6 @@ class MainPage extends Component { firebaseClient.send(JSON.stringify(body), "notification"); } - sendRemoteData(token) { - let body = { - "to": token, - "data":{ - "title": "Simple FCM Client", - "body": "This is a notification with only DATA.", - "sound": "default" - }, - "priority": "normal" - } - - firebaseClient.send(JSON.stringify(body), "data"); - } - - showLocalNotificationWithAction() { - // FCM.presentLocalNotification({ - // title: 'Test Notification with action', - // body: 'Force touch to reply', - // priority: "high", - // show_in_foreground: true, - // click_action: "com.myidentifi.fcm.text", // for ios - // android_actions: JSON.stringify([{ - // id: "view", - // title: 'view' - // },{ - // id: "dismiss", - // title: 'dismiss' - // }]) // for android, take syntax similar to ios's. only buttons are supported - // }); - } - render() { let { token, tokenCopyFeedback } = this.state; @@ -225,18 +180,10 @@ class MainPage extends Component { Send Remote Notification - this.sendRemoteData(token)} style={styles.button}> - Send Remote Data - - this.showLocalNotification()} style={styles.button}> Show Local Notification - this.showLocalNotificationWithAction(token)} style={styles.button}> - Show Local Notification with Action - - this.scheduleLocalNotification()} style={styles.button}> Schedule Notification in 5s @@ -245,7 +192,7 @@ class MainPage extends Component { Init notif: - {JSON.stringify(this.state.initNotif)} + {JSON.stringify(this.state.initNotif && this.state.initNotif.data)} diff --git a/Examples/firebase-migration/app/Listeners.js b/Examples/firebase-migration/app/Listeners.js index 060a5a6..8751894 100644 --- a/Examples/firebase-migration/app/Listeners.js +++ b/Examples/firebase-migration/app/Listeners.js @@ -16,16 +16,19 @@ export function registerKilledListener(message: RemoteMessage){ // these callback will be triggered only when app is foreground or background export function registerAppListener(navigation){ + this.notificationListener = firebase.notifications().onNotification(notification => { + firebase.notifications().displayNotification(notification); + }) this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => { const notif: Notification = notificationOpen.notification; - if(notif.targetScreen === 'detail'){ + if(notif.data.targetScreen === 'detail'){ setTimeout(()=>{ navigation.navigate('Detail') }, 500) } setTimeout(()=>{ - alert(`User tapped notification\n${JSON.stringify(notif)}`) + alert(`User tapped notification\n${notif.notificationId}`) }, 500) }); @@ -34,7 +37,16 @@ export function registerAppListener(navigation){ }); this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => { - // Process your message as required + if(message.data && message.data.custom_notification){ + let notification = new firebase.notifications.Notification(); + notification = notification.setNotificationId(new Date().valueOf().toString()) + .setTitle(message.title) + .setBody(message.body) + .setData(message.data) + .setSound("bell.mp3") + notification.android.setChannelId("test-channel") + firebase.notifications().displayNotification(notification); + } }); } diff --git a/Examples/firebase-migration/index.android.js b/Examples/firebase-migration/index.android.js index abd8111..32594e5 100644 --- a/Examples/firebase-migration/index.android.js +++ b/Examples/firebase-migration/index.android.js @@ -5,6 +5,8 @@ */ import React, { Component } from 'react'; +import {registerKilledListener} from './app/Listeners'; + import { AppRegistry, StyleSheet, @@ -21,3 +23,5 @@ export default class SimpleFcmClient extends Component { } AppRegistry.registerComponent('SimpleFcmClient', () => SimpleFcmClient); + +AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => registerKilledListener); diff --git a/Examples/firebase-migration/index.ios.js b/Examples/firebase-migration/index.ios.js index ae23b89..c1c1ffd 100644 --- a/Examples/firebase-migration/index.ios.js +++ b/Examples/firebase-migration/index.ios.js @@ -5,7 +5,6 @@ */ import React, { Component } from 'react'; -import {registerKilledListener} from './app/Listeners'; import { AppRegistry, @@ -22,6 +21,4 @@ export default class SimpleFcmClient extends Component { } } -AppRegistry.registerComponent('SimpleFcmClient', () => SimpleFcmClient); - -AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => registerKilledListener); \ No newline at end of file +AppRegistry.registerComponent('SimpleFcmClient', () => SimpleFcmClient); \ No newline at end of file diff --git a/Examples/firebase-migration/ios/SimpleFcmClient/AppDelegate.m b/Examples/firebase-migration/ios/SimpleFcmClient/AppDelegate.m index 704f61c..fb58dc0 100644 --- a/Examples/firebase-migration/ios/SimpleFcmClient/AppDelegate.m +++ b/Examples/firebase-migration/ios/SimpleFcmClient/AppDelegate.m @@ -12,6 +12,7 @@ #import #import "RCTRootView.h" +#import #import "RNFirebaseNotifications.h" #import "RNFirebaseMessaging.h" @@ -19,6 +20,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + [FIRApp configure]; NSURL *jsCodeLocation; jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; -- 2.26.2