From 4b1cd17b40229e67504f5dcc5990af7dfdd39004 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Wed, 20 Dec 2017 12:23:09 -0500 Subject: [PATCH 1/4] add channel --- .../android/app/build.gradle | 10 ++-- .../simple-fcm-client/android/build.gradle | 3 ++ Examples/simple-fcm-client/app/App.js | 7 +++ android/build.gradle | 5 +- .../evollu/react/fcm/FIRMessagingModule.java | 46 +++++++++++++++++++ .../react/fcm/SendNotificationTask.java | 2 +- index.js | 6 +++ 7 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Examples/simple-fcm-client/android/app/build.gradle b/Examples/simple-fcm-client/android/app/build.gradle index 87c9dc55..f1ca431b 100644 --- a/Examples/simple-fcm-client/android/app/build.gradle +++ b/Examples/simple-fcm-client/android/app/build.gradle @@ -84,13 +84,13 @@ def enableSeparateBuildPerCPUArchitecture = false def enableProguardInReleaseBuilds = false android { - compileSdkVersion 23 + compileSdkVersion 26 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.google.firebase.quickstart.fcm" minSdkVersion 16 - targetSdkVersion 22 + targetSdkVersion 26 versionCode 1 versionName "1.0" ndk { @@ -124,12 +124,16 @@ android { } } } + configurations.all { + resolutionStrategy.force 'com.android.support:support-core-utils:26.1.0' + resolutionStrategy.force 'com.android.support:support-core-ui:26.1.0' + } } dependencies { compile project(':react-native-fcm') compile fileTree(dir: "libs", include: ["*.jar"]) - compile "com.android.support:appcompat-v7:23.0.1" + compile "com.android.support:appcompat-v7:26.1.0" compile "com.facebook.react:react-native:+" // From node_modules } diff --git a/Examples/simple-fcm-client/android/build.gradle b/Examples/simple-fcm-client/android/build.gradle index 7b16caa1..eced6ef3 100644 --- a/Examples/simple-fcm-client/android/build.gradle +++ b/Examples/simple-fcm-client/android/build.gradle @@ -21,5 +21,8 @@ allprojects { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } + maven { + url "https://maven.google.com" + } } } diff --git a/Examples/simple-fcm-client/app/App.js b/Examples/simple-fcm-client/app/App.js index 944efb6d..9f9f3824 100644 --- a/Examples/simple-fcm-client/app/App.js +++ b/Examples/simple-fcm-client/app/App.js @@ -32,6 +32,12 @@ export default class App extends Component { } async componentDidMount(){ + FCM.createNotificationChannel({ + id: 'default', + name: 'Default', + description: 'used for example', + priority: 'high' + }) registerAppListener(); FCM.getInitialNotification().then(notif => { this.setState({ @@ -61,6 +67,7 @@ export default class App extends Component { FCM.presentLocalNotification({ vibrate: 500, title: 'Hello', + channel: 'default', body: 'Test Notification', big_text: 'i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large', priority: "high", diff --git a/android/build.gradle b/android/build.gradle index 8f6ed761..8a01f4b9 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 + compileSdkVersion 26 buildToolsVersion "25.0.2" defaultConfig { minSdkVersion 16 - targetSdkVersion 25 + targetSdkVersion 26 versionCode 1 versionName "1.0" } @@ -22,5 +22,6 @@ dependencies { compile 'com.google.firebase:firebase-core:+' compile 'com.google.firebase:firebase-messaging:+' compile 'me.leolin:ShortcutBadger:1.1.17@aar' + compile "com.android.support:support-core-utils:26.1.0" } diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index a50f0bbb..ff426b82 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -1,6 +1,8 @@ package com.evollu.react.fcm; import android.app.Activity; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; @@ -23,6 +25,7 @@ import com.google.firebase.messaging.RemoteMessage.Notification; import android.app.Application; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.NotificationManagerCompat; import android.support.v4.content.LocalBroadcastManager; @@ -35,6 +38,8 @@ import java.util.Set; import java.util.UUID; +import static android.content.Context.NOTIFICATION_SERVICE; + public class FIRMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener { private final static String TAG = FIRMessagingModule.class.getCanonicalName(); private FIRLocalMessagingHelper mFIRLocalMessagingHelper; @@ -75,6 +80,47 @@ public void requestPermissions(Promise promise){ } } + @ReactMethod + public void createNotificationChannel(ReadableMap details, Promise promise){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationManager mngr = (NotificationManager) getReactApplicationContext().getSystemService(NOTIFICATION_SERVICE); + String id = details.getString("id"); + String name = details.getString("name"); + String priority = details.getString("priority"); + int importance; + switch(priority) { + case "min": + importance = NotificationManager.IMPORTANCE_MIN; + break; + case "low": + importance = NotificationManager.IMPORTANCE_LOW; + break; + case "high": + importance = NotificationManager.IMPORTANCE_HIGH; + break; + case "max": + importance = NotificationManager.IMPORTANCE_MAX; + break; + default: + importance = NotificationManager.IMPORTANCE_DEFAULT; + } + if (mngr.getNotificationChannel(id) != null) { + promise.resolve(null); + } + // + NotificationChannel channel = new NotificationChannel( + id, + name, + importance); + // Configure the notification channel. + if(details.hasKey("description")){ + channel.setDescription(details.getString("description")); + } + mngr.createNotificationChannel(channel); + } + promise.resolve(null); + } + @ReactMethod public void getFCMToken(Promise promise) { try { diff --git a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java index e34c5a3a..37496238 100644 --- a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java +++ b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java @@ -62,7 +62,7 @@ protected Void doInBackground(Void... params) { title = mContext.getPackageManager().getApplicationLabel(appInfo).toString(); } - NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext) + NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, bundle.getString("channel")) .setContentTitle(title) .setContentText(bundle.getString("body")) .setTicker(bundle.getString("ticker")) diff --git a/index.js b/index.js index dd0b3cb0..86088f51 100644 --- a/index.js +++ b/index.js @@ -62,6 +62,12 @@ FCM.requestPermissions = () => { return RNFIRMessaging.requestPermissions(); }; +FCM.createNotificationChannel = (channel) => { + if (Platform.OS === 'android') { + return RNFIRMessaging.createNotificationChannel(); + } +} + FCM.presentLocalNotification = (details) => { details.id = details.id || new Date().getTime().toString(); details.local_notification = true; From aa34372a63db3cdaaa07ebdb81fa9812c3356d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20H=C3=A4ggholm?= Date: Thu, 4 Jan 2018 12:18:17 -0800 Subject: [PATCH 2/4] Pass channel options to native module --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 86088f51..0fd049eb 100644 --- a/index.js +++ b/index.js @@ -64,7 +64,7 @@ FCM.requestPermissions = () => { FCM.createNotificationChannel = (channel) => { if (Platform.OS === 'android') { - return RNFIRMessaging.createNotificationChannel(); + return RNFIRMessaging.createNotificationChannel(channel); } } From ce35f3274c6f637b38e1b071d005d2c4daeddedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20H=C3=A4ggholm?= Date: Thu, 4 Jan 2018 12:18:35 -0800 Subject: [PATCH 3/4] Avoid resolving promise twice if channel already exists --- .../src/main/java/com/evollu/react/fcm/FIRMessagingModule.java | 1 + 1 file changed, 1 insertion(+) diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index ff426b82..adee2a88 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -106,6 +106,7 @@ public void createNotificationChannel(ReadableMap details, Promise promise){ } if (mngr.getNotificationChannel(id) != null) { promise.resolve(null); + return; } // NotificationChannel channel = new NotificationChannel( From c3466ab464080d76c7f36ee3dc943e409f5ab062 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Thu, 3 May 2018 14:01:08 -0400 Subject: [PATCH 4/4] bump SDK version --- Examples/simple-fcm-client/android/app/build.gradle | 7 +++---- Examples/simple-fcm-client/android/build.gradle | 11 ++++++++--- .../android/gradle/wrapper/gradle-wrapper.properties | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Examples/simple-fcm-client/android/app/build.gradle b/Examples/simple-fcm-client/android/app/build.gradle index f1ca431b..d13dcafd 100644 --- a/Examples/simple-fcm-client/android/app/build.gradle +++ b/Examples/simple-fcm-client/android/app/build.gradle @@ -84,13 +84,13 @@ def enableSeparateBuildPerCPUArchitecture = false def enableProguardInReleaseBuilds = false android { - compileSdkVersion 26 - buildToolsVersion "23.0.1" + compileSdkVersion 27 + buildToolsVersion "27.0.3" defaultConfig { applicationId "com.google.firebase.quickstart.fcm" minSdkVersion 16 - targetSdkVersion 26 + targetSdkVersion 27 versionCode 1 versionName "1.0" ndk { @@ -133,7 +133,6 @@ android { dependencies { compile project(':react-native-fcm') compile fileTree(dir: "libs", include: ["*.jar"]) - compile "com.android.support:appcompat-v7:26.1.0" compile "com.facebook.react:react-native:+" // From node_modules } diff --git a/Examples/simple-fcm-client/android/build.gradle b/Examples/simple-fcm-client/android/build.gradle index eced6ef3..b0242a0e 100644 --- a/Examples/simple-fcm-client/android/build.gradle +++ b/Examples/simple-fcm-client/android/build.gradle @@ -3,10 +3,14 @@ buildscript { repositories { jcenter() + maven { + url 'https://maven.google.com/' + name '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 @@ -22,7 +26,8 @@ allprojects { url "$rootDir/../node_modules/react-native/android" } maven { - url "https://maven.google.com" + url 'https://maven.google.com/' + name 'Google' } } } diff --git a/Examples/simple-fcm-client/android/gradle/wrapper/gradle-wrapper.properties b/Examples/simple-fcm-client/android/gradle/wrapper/gradle-wrapper.properties index a27341b3..02df1b0c 100644 --- a/Examples/simple-fcm-client/android/gradle/wrapper/gradle-wrapper.properties +++ b/Examples/simple-fcm-client/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Jan 06 16:34:59 EST 2017 +#Thu May 03 13:49:29 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