Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add channel #699

Merged
merged 13 commits into from
Jun 13, 2018
10 changes: 7 additions & 3 deletions Examples/simple-fcm-client/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,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 27
versionCode 1
versionName "1.0"
ndk {
Expand Down Expand Up @@ -124,6 +124,10 @@ 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 {
Expand Down
12 changes: 10 additions & 2 deletions Examples/simple-fcm-client/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,5 +25,9 @@ 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/'
name 'Google'
}
}
}
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions Examples/simple-fcm-client/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class MainPage extends Component {
}

async componentDidMount() {
FCM.createNotificationChannel({
id: 'default',
name: 'Default',
description: 'used for example',
priority: 'high'
})
registerAppListener(this.props.navigation);
FCM.getInitialNotification().then(notif => {
this.setState({
Expand Down Expand Up @@ -75,6 +81,7 @@ class MainPage extends Component {

showLocalNotification() {
FCM.presentLocalNotification({
channel: 'default',
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)
Expand Down
7 changes: 4 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 25
def DEFAULT_COMPILE_SDK_VERSION = 26
def DEFAULT_BUILD_TOOLS_VERSION = "25.0.2"
def DEFAULT_TARGET_SDK_VERSION = 25
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "11.+"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "12.+"

android {
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
Expand All @@ -29,5 +29,6 @@ dependencies {
compile "com.google.firebase:firebase-core:$googlePlayServicesVersion"
compile "com.google.firebase:firebase-messaging:$googlePlayServicesVersion"
compile 'me.leolin:ShortcutBadger:1.1.17@aar'
compile "com.android.support:support-core-utils:26.1.0"
}

47 changes: 47 additions & 0 deletions android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -36,6 +39,8 @@
import java.util.UUID;
import com.google.firebase.FirebaseApp;

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;
Expand Down Expand Up @@ -76,6 +81,48 @@ 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);
return;
}
//
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected Void doInBackground(Void... params) {
String subText = bundle.getString("sub_text");
if (subText != null) subText = URLDecoder.decode( subText, "UTF-8" );

NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext)
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, bundle.getString("channel"))
.setContentTitle(title)
.setContentText(body)
.setTicker(ticker)
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ FCM.requestPermissions = () => {
return RNFIRMessaging.requestPermissions();
};

FCM.createNotificationChannel = (channel) => {
if (Platform.OS === 'android') {
return RNFIRMessaging.createNotificationChannel(channel);
}
}

FCM.presentLocalNotification = (details) => {
details.id = details.id || new Date().getTime().toString();
details.local_notification = true;
Expand Down