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

Added support for notification channels #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/android/FirebaseMessagingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
import static com.google.android.gms.tasks.Tasks.await;
import static by.chemerisuk.cordova.support.ExecutionThread.WORKER;

//--- begin: notification channel imports (GLS)
import android.app.NotificationChannel;
import android.media.AudioAttributes;
import android.content.ContentResolver;
import org.json.JSONArray;
import java.util.List;
import android.os.Build;
//---- end: notification channel imports

public class FirebaseMessagingPlugin extends ReflectiveCordovaPlugin {
private static final String TAG = "FCMPlugin";
Expand Down Expand Up @@ -242,4 +250,112 @@ private static JSONObject toJSON(RemoteMessage.Notification notification) throws

return result;
}

//----------------------------------------------------------------------------------
//---- begin: notification channel implementation (GLS)
//----------------------------------------------------------------------------------
private static JSONObject toJSON(NotificationChannel channel) throws JSONException {
JSONObject result = new JSONObject()
.put("id", channel.getId())
.put("name", channel.getName())
.put("description", channel.getDescription())
.put("importance", channel.getImportance())
.put("badge", channel.canShowBadge())
.put("light", channel.shouldShowLights())
.put("lightColor", channel.getLightColor())
.put("sound", channel.getSound())
.put("vibration", channel.getVibrationPattern());

return result;
}

@CordovaMethod
private void createChannel(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
throw new UnsupportedOperationException("Notification channels are not supported");
}
JSONObject options = args.getJSONObject(0);

String channelId = options.getString("id");
String channelName = options.getString("name");
int importance = options.getInt("importance");
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setDescription(options.optString("description", ""));
channel.setShowBadge(options.optBoolean("badge", true));

channel.enableLights(options.optBoolean("light", false));
channel.setLightColor(options.optInt("lightColor", 0));

String soundName = options.optString("sound", "default");
if (!"default".equals(soundName)) {
String packageName = cordova.getActivity().getPackageName();
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + soundName);
channel.setSound(soundUri, new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build());
}

JSONArray vibrationPattern = options.optJSONArray("vibration");
if (vibrationPattern != null) {
int patternLength = vibrationPattern.length();
long[] patternArray = new long[patternLength];
for (int i = 0; i < patternLength; i++) {
patternArray[i] = vibrationPattern.getLong(i);
}
channel.setVibrationPattern(patternArray);
channel.enableVibration(true);
} else {
channel.enableVibration(options.optBoolean("vibration", true));
}

notificationManager.createNotificationChannel(channel);

callbackContext.success();
}

@CordovaMethod
private void findChannel(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
String channelId = args.getString(0);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
throw new UnsupportedOperationException("Notification channels are not supported");
}

NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
if (channel == null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, (String)null));
} else {
callbackContext.success(toJSON(channel));
}
}

@CordovaMethod
private void listChannels(CallbackContext callbackContext) throws JSONException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
throw new UnsupportedOperationException("Notification channels are not supported");
}

List<NotificationChannel> channels = notificationManager.getNotificationChannels();
JSONArray result = new JSONArray();
for (NotificationChannel channel : channels) {
result.put(toJSON(channel));
}

callbackContext.success(result);
}

@CordovaMethod
private void deleteChannel(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
String channelId = args.getString(0);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
throw new UnsupportedOperationException("Notification channels are not supported");
}

notificationManager.deleteNotificationChannel(channelId);

callbackContext.success();
}
//----------------------------------------------------------------------------------
//---- end: notification channel implementation (GLS)
//----------------------------------------------------------------------------------
}
56 changes: 43 additions & 13 deletions www/FirebaseMessaging.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var PLUGIN_NAME = "FirebaseMessaging";
cordova.define("cordova-plugin-firebase-messaging.FirebaseMessaging", function(require, exports, module) {
var PLUGIN_NAME = "FirebaseMessaging";
// @ts-ignore
var exec = require("cordova/exec");
var exec = cordova.require("cordova/exec");
// import exec from "cordova/exec";
// var exec = cordova.exec;
let _exports = module.exports

/**
*
Expand All @@ -16,7 +20,7 @@ var exec = require("cordova/exec");
* @property {Record<string, any>} [aps] IOS payload, available when message arrives in both foreground and background.
*/

exports.subscribe =
_exports.subscribe =
/**
*
* Subscribe to a FCM topic.
Expand All @@ -32,7 +36,7 @@ function(topic) {
});
};

exports.unsubscribe =
_exports.unsubscribe =
/**
*
* Unsubscribe from a FCM topic.
Expand All @@ -48,7 +52,7 @@ function(topic) {
});
};

exports.onTokenRefresh =
_exports.onTokenRefresh =
/**
*
* Registers callback to notify when FCM token is updated.
Expand All @@ -66,7 +70,7 @@ function(callback, errorCallback) {
exec(callback, errorCallback, PLUGIN_NAME, "onTokenRefresh", []);
};

exports.onMessage =
_exports.onMessage =
/**
*
* Registers foreground push notification callback.
Expand All @@ -82,7 +86,7 @@ function(callback, errorCallback) {
exec(callback, errorCallback, PLUGIN_NAME, "onMessage", []);
};

exports.onBackgroundMessage =
_exports.onBackgroundMessage =
/**
*
* Registers background push notification callback.
Expand All @@ -98,7 +102,7 @@ function(callback, errorCallback) {
exec(callback, errorCallback, PLUGIN_NAME, "onBackgroundMessage", []);
};

exports.clearNotifications =
_exports.clearNotifications =
/**
*
* Clear all notifications from system notification bar.
Expand All @@ -113,7 +117,7 @@ function() {
});
};

exports.deleteToken =
_exports.deleteToken =
/**
*
* Delete the Instance ID (Token) and the data associated with it.
Expand All @@ -130,7 +134,7 @@ function() {
});
};

exports.getToken =
_exports.getToken =
/**
*
* Returns the current FCM token.
Expand All @@ -148,7 +152,7 @@ function(format) {
});
};

exports.setBadge =
_exports.setBadge =
/**
*
* Sets current badge number (if supported).
Expand All @@ -164,7 +168,7 @@ function(badgeValue) {
});
};

exports.getBadge =
_exports.getBadge =
/**
*
* Gets current badge number (if supported).
Expand All @@ -181,7 +185,31 @@ function() {
});
};

exports.requestPermission =
//--------------------------------------------------------
//------------- GLS
_exports.findChannel = function(channelId) {
return new Promise(function(resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "findChannel", [channelId]);
});
},
_exports.listChannels = function() {
return new Promise(function(resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "listChannels", []);
});
},
_exports.createChannel = function(options) {
return new Promise(function(resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "createChannel", [options]);
});
},
_exports.deleteChannel = function(channelId) {
return new Promise(function(resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "deleteChannel", [channelId]);
});
}
//--------------------------------------------------------

_exports.requestPermission =
/**
*
* Ask for permission to recieve push notifications (will trigger prompt on iOS).
Expand All @@ -205,3 +233,5 @@ function(options) {
exec(resolve, reject, PLUGIN_NAME, "requestPermission", [options || {}]);
});
};

});