From a0d2c3b6f8fb61daf21940e4bec85f00ecadae2b Mon Sep 17 00:00:00 2001 From: Grumpy Gary Date: Tue, 10 Jan 2023 07:17:45 -0600 Subject: [PATCH] Added support for notification channels --- src/android/FirebaseMessagingPlugin.java | 116 +++++++++++++++++++++++ www/FirebaseMessaging.js | 56 ++++++++--- 2 files changed, 159 insertions(+), 13 deletions(-) diff --git a/src/android/FirebaseMessagingPlugin.java b/src/android/FirebaseMessagingPlugin.java index 47ff1ab..edb0310 100644 --- a/src/android/FirebaseMessagingPlugin.java +++ b/src/android/FirebaseMessagingPlugin.java @@ -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"; @@ -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 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) + //---------------------------------------------------------------------------------- } diff --git a/www/FirebaseMessaging.js b/www/FirebaseMessaging.js index 3fcf5e7..fc7a907 100644 --- a/www/FirebaseMessaging.js +++ b/www/FirebaseMessaging.js @@ -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 /** * @@ -16,7 +20,7 @@ var exec = require("cordova/exec"); * @property {Record} [aps] IOS payload, available when message arrives in both foreground and background. */ -exports.subscribe = +_exports.subscribe = /** * * Subscribe to a FCM topic. @@ -32,7 +36,7 @@ function(topic) { }); }; -exports.unsubscribe = +_exports.unsubscribe = /** * * Unsubscribe from a FCM topic. @@ -48,7 +52,7 @@ function(topic) { }); }; -exports.onTokenRefresh = +_exports.onTokenRefresh = /** * * Registers callback to notify when FCM token is updated. @@ -66,7 +70,7 @@ function(callback, errorCallback) { exec(callback, errorCallback, PLUGIN_NAME, "onTokenRefresh", []); }; -exports.onMessage = +_exports.onMessage = /** * * Registers foreground push notification callback. @@ -82,7 +86,7 @@ function(callback, errorCallback) { exec(callback, errorCallback, PLUGIN_NAME, "onMessage", []); }; -exports.onBackgroundMessage = +_exports.onBackgroundMessage = /** * * Registers background push notification callback. @@ -98,7 +102,7 @@ function(callback, errorCallback) { exec(callback, errorCallback, PLUGIN_NAME, "onBackgroundMessage", []); }; -exports.clearNotifications = +_exports.clearNotifications = /** * * Clear all notifications from system notification bar. @@ -113,7 +117,7 @@ function() { }); }; -exports.deleteToken = +_exports.deleteToken = /** * * Delete the Instance ID (Token) and the data associated with it. @@ -130,7 +134,7 @@ function() { }); }; -exports.getToken = +_exports.getToken = /** * * Returns the current FCM token. @@ -148,7 +152,7 @@ function(format) { }); }; -exports.setBadge = +_exports.setBadge = /** * * Sets current badge number (if supported). @@ -164,7 +168,7 @@ function(badgeValue) { }); }; -exports.getBadge = +_exports.getBadge = /** * * Gets current badge number (if supported). @@ -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). @@ -205,3 +233,5 @@ function(options) { exec(resolve, reject, PLUGIN_NAME, "requestPermission", [options || {}]); }); }; + +}); \ No newline at end of file