From 50ea463e896173749606aa39c6118797f7a40eab Mon Sep 17 00:00:00 2001 From: prashant000chaudhary <42371725+prashant000chaudhary@users.noreply.github.com> Date: Thu, 22 Nov 2018 13:12:30 +0530 Subject: [PATCH 01/24] Added timestamp while storing mark as read data --- src/android/HttpPost.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/android/HttpPost.java b/src/android/HttpPost.java index 461c019b3..bfb16207e 100644 --- a/src/android/HttpPost.java +++ b/src/android/HttpPost.java @@ -194,8 +194,10 @@ public Message(String target,String message) { private class MarkAsRead { String target; + long timestamp; public MarkAsRead(String target) { this.target = target; + this.timestamp = new Date().getTime(); } } From c9d9ebd61b253635f02b1380efb068537408fd2b Mon Sep 17 00:00:00 2001 From: iKh Date: Fri, 23 Nov 2018 11:03:50 +0200 Subject: [PATCH 02/24] API to schedule local notifications (#1) --- src/android/FirebasePlugin.java | 37 +++++++- .../FirebasePluginMessagingService.java | 93 ++++++++++--------- www/firebase.js | 4 + 3 files changed, 87 insertions(+), 47 deletions(-) diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index 72475e21a..92e96e8af 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -205,14 +205,17 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo this.setAnalyticsCollectionEnabled(callbackContext, args.getBoolean(0)); return true; } else if (action.equals("setPerformanceCollectionEnabled")) { - this.setPerformanceCollectionEnabled(callbackContext, args.getBoolean(0)); - return true; + this.setPerformanceCollectionEnabled(callbackContext, args.getBoolean(0)); + return true; } else if (action.equals("clearAllNotifications")) { this.clearAllNotifications(callbackContext); return true; } else if (action.equals("clear")) { this.clear(callbackContext, args.getInt(0)); return true; + } else if (action.equals("scheduleLocalNotification")) { + this.scheduleLocalNotification(callbackContext, args.getJSONObject(0)); + return true; } return false; @@ -1124,4 +1127,34 @@ public void run() { } }); } + + public void scheduleLocalNotification(final CallbackContext callbackContext, final JSONObject params) { + cordova.getThreadPool().execute(new Runnable() { + public void run() { + try { + Context activityContext = cordova.getActivity(); + Context appContext = activityContext.getApplicationContext(); + + String id = params.getString("id"); + String target = params.getString("target"); + String username = params.getString("username"); + String groupName = params.getString("groupName"); + String message = params.getString("message"); + String eventType = params.getString("eventType"); + String nsound = params.getString("nsound"); + String sound = params.getString("sound"); + String lights = params.getString("lights"); + + FirebasePluginMessagingService.displayNotification(activityContext, appContext, id, target, username, groupName, message, eventType, nsound, true, sound, lights); + + callbackContext.success(); + } catch (Exception e) { + if(FirebasePlugin.crashlyticsInit()){ + Crashlytics.log(e.getMessage()); + } + callbackContext.error(e.getMessage()); + } + } + }); + } } diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 6b47f18f6..f06ac0bc2 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -49,10 +49,10 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String VNC_PEER_JID = "vncPeerJid"; private static final String NOTIFY_ID = "id"; - private String getStringResource(String name) { - return this.getString( - this.getResources().getIdentifier( - name, "string", this.getPackageName() + private static String getStringResource(Context activityOrServiceContext, String name) { + return activityOrServiceContext.getString( + activityOrServiceContext.getResources().getIdentifier( + name, "string", activityOrServiceContext.getPackageName() ) ); } @@ -132,21 +132,13 @@ public void onMessageReceived(RemoteMessage remoteMessage) { String eventType = notification.eType; String nsound = notification.nsound; - Log.d(TAG, "Notification id: " + id); - Log.d(TAG, "Notification Target: " + target); - Log.d(TAG, "Notification username: " + username); - Log.d(TAG, "Notification groupName: " + groupName); - Log.d(TAG, "Notification message: " + message); - Log.d(TAG, "Notification eventType: " + eventType); - Log.d(TAG, "Notification nsound: " + nsound); - if (TextUtils.isEmpty(target) || TextUtils.isEmpty(username)) { - Log.d(TAG, "returning due to empty values"); + Log.d(TAG, "returning due to empty 'target' or 'username' values"); return; } boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()); - sendNotification(id, target, username, groupName, message, eventType, nsound, showNotification, "", ""); + displayNotification(this, getApplicationContext(), id, target, username, groupName, message, eventType, nsound, showNotification, "", ""); try { StringBuffer output = new StringBuffer(); @@ -190,7 +182,18 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } } - private void sendNotification(String id, String target, String name, String groupName, String message, String eventType, String nosound, boolean showNotification, String sound, String lights) { + public static void displayNotification(Context activityOrServiceContext, Context appContext, String id, String target, String name, String groupName, String message, String eventType, String nsound, boolean showNotification, String sound, String lights) { + Log.d(TAG, "displayNotification: id: " + id); + Log.d(TAG, "displayNotification: Target: " + target); + Log.d(TAG, "displayNotification: username: " + name); + Log.d(TAG, "displayNotification: groupName: " + groupName); + Log.d(TAG, "displayNotification: message: " + message); + Log.d(TAG, "displayNotification: eventType: " + eventType); + Log.d(TAG, "displayNotification: nsound: " + nsound); + Log.d(TAG, "displayNotification: showNotification: " + showNotification); + Log.d(TAG, "displayNotification: sound: " + sound); + Log.d(TAG, "displayNotification: lights: " + lights); + if (!showNotification) { return; } @@ -206,29 +209,29 @@ private void sendNotification(String id, String target, String name, String grou if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Log.i("VNC", "Create notification actions (>=N), NOTIFY_ID: " + id); - replyPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), + replyPendingIntent = PendingIntent.getBroadcast(appContext, REQUEST_CODE_HELP, - new Intent(this, NotificationReceiver.class) + new Intent(activityOrServiceContext, NotificationReceiver.class) .setAction(inlineReplyActionName), 0); - markAsReadPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), + markAsReadPendingIntent = PendingIntent.getBroadcast(appContext, REQUEST_CODE_HELP, - new Intent(this, NotificationReceiver.class) + new Intent(activityOrServiceContext, NotificationReceiver.class) .setAction(markAsReadActionName), 0); } else { Log.i("VNC", "Create notification actions, NOTIFY_ID: " + id); - replyPendingIntent = PendingIntent.getActivity(getApplicationContext(), + replyPendingIntent = PendingIntent.getActivity(appContext, REQUEST_CODE_HELP, - new Intent(this, ReplyActivity.class) + new Intent(activityOrServiceContext, ReplyActivity.class) .setAction(inlineReplyActionName), 0); - markAsReadPendingIntent = PendingIntent.getActivity(getApplicationContext(), + markAsReadPendingIntent = PendingIntent.getActivity(appContext, REQUEST_CODE_HELP, - new Intent(this, ReplyActivity.class) + new Intent(activityOrServiceContext, ReplyActivity.class) .setAction(markAsReadActionName), 0); } @@ -244,23 +247,23 @@ private void sendNotification(String id, String target, String name, String grou android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) .build(); - Log.d(TAG, "going to show notification with " + nosound); + Log.d(TAG, "going to show notification with " + nsound); - Intent intent = new Intent(this, OnNotificationOpenReceiver.class); + Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); Bundle bundle = new Bundle(); bundle.putString(VNC_PEER_JID, target); bundle.putString("vncEventType", "chat"); bundle.putInt(NOTIFY_ID, notificationId); intent.putExtras(bundle); - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Integer.parseInt(id), intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getBroadcast(activityOrServiceContext, Integer.parseInt(id), intent, PendingIntent.FLAG_UPDATE_CURRENT); - String channelId = this.getStringResource("default_notification_channel_id"); - String channelName = this.getStringResource("default_notification_channel_name"); + String channelId = getStringResource(activityOrServiceContext, "default_notification_channel_id"); + String channelName = getStringResource(activityOrServiceContext, "default_notification_channel_name"); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - if (nosound.equals("mute")) { - Log.d(TAG, "notification nosound - switching channel"); - channelId = this.getStringResource("silent_notification_channel_id"); - channelName = this.getStringResource("silent_notification_channel_name"); + if (nsound.equals("mute")) { + Log.d(TAG, "notification nsound - switching channel"); + channelId = getStringResource(activityOrServiceContext, "silent_notification_channel_id"); + channelName = getStringResource(activityOrServiceContext, "silent_notification_channel_name"); defaultSoundUri = null; } @@ -279,10 +282,10 @@ private void sendNotification(String id, String target, String name, String grou Log.d(TAG, "Notification group name: " + title); - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId); + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(activityOrServiceContext, channelId); - if (nosound.equals("mute")) { - Log.d(TAG, "notification nosound: " + title); + if (nsound.equals("mute")) { + Log.d(TAG, "notification nsound: " + title); notificationBuilder .setDefaults(NotificationCompat.DEFAULT_VIBRATE) @@ -317,19 +320,19 @@ private void sendNotification(String id, String target, String name, String grou notificationBuilder.addAction(actionMarkAsRead); } - int resID = getResources().getIdentifier("logo", "drawable", getPackageName()); + int resID = activityOrServiceContext.getResources().getIdentifier("logo", "drawable", activityOrServiceContext.getPackageName()); if (resID != 0) { notificationBuilder.setSmallIcon(resID); } else { - notificationBuilder.setSmallIcon(getApplicationInfo().icon); + notificationBuilder.setSmallIcon(activityOrServiceContext.getApplicationInfo().icon); } - if (nosound.equals("mute")) { + if (nsound.equals("mute")) { Log.d(TAG, "not setting sound"); } else { if (sound != null) { Log.d(TAG, "sound before path is: " + sound); - Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/" + sound); + Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + activityOrServiceContext.getPackageName() + "/raw/" + sound); Log.d(TAG, "Parsed sound is: " + soundPath.toString()); notificationBuilder.setSound(soundPath); } else { @@ -352,24 +355,24 @@ private void sendNotification(String id, String target, String name, String grou } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { - int accentID = getResources().getIdentifier("accent", "color", getPackageName()); - notificationBuilder.setColor(getResources().getColor(accentID, null)); + int accentID = activityOrServiceContext.getResources().getIdentifier("accent", "color", activityOrServiceContext.getPackageName()); + notificationBuilder.setColor(activityOrServiceContext.getResources().getColor(accentID, null)); } Notification notification = notificationBuilder.build(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { int iconID = android.R.id.icon; - int notiID = getResources().getIdentifier("icon" + - "", "mipmap", getPackageName()); + int notiID = activityOrServiceContext.getResources().getIdentifier("icon" + + "", "mipmap", activityOrServiceContext.getPackageName()); if (notification.contentView != null) { notification.contentView.setImageViewResource(iconID, notiID); } } - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager notificationManager = (NotificationManager) activityOrServiceContext.getSystemService(Context.NOTIFICATION_SERVICE); // Since android Oreo notification channel is needed. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - if (nosound.equals("mute")) { + if (nsound.equals("mute")) { Log.d(TAG, "pushing to silentchannel"); NotificationChannel silentchannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); silentchannel.setSound(null, null); diff --git a/www/firebase.js b/www/firebase.js index 99d647971..0d0883502 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -179,3 +179,7 @@ exports.clearAllNotifications = function (success, error) { exports.clear = function (id, success, error) { exec(success, error, "FirebasePlugin", "clear", [id]); }; + +exports.scheduleLocalNotification = function (params, success, error) { + exec(success, error, "FirebasePlugin", "scheduleLocalNotification", [params]); +}; From cfdfc1f178f27a6edb3021fa94a81f722a407618 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 27 Nov 2018 16:02:26 +0200 Subject: [PATCH 03/24] Review of notifications ids storing logic --- .../FirebasePluginMessagingService.java | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index f06ac0bc2..fc347c24b 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -69,28 +69,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) { return; } - File file = new File(this.getFilesDir(), FILE_NAME); - - FileReader fileReader = null; - FileWriter fileWriter = null; - BufferedReader bufferedReader = null; - BufferedWriter bufferedWriter = null; - - String response = null; - - if (!file.exists()) { - try { - file.createNewFile(); - fileWriter = new FileWriter(file.getAbsoluteFile()); - bufferedWriter = new BufferedWriter(fileWriter); - bufferedWriter.write("{}"); - bufferedWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - // [START_EXCLUDE] // There are two types of messages data messages and notification messages. Data messages are handled // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type @@ -139,42 +117,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) { boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()); displayNotification(this, getApplicationContext(), id, target, username, groupName, message, eventType, nsound, showNotification, "", ""); - - try { - StringBuffer output = new StringBuffer(); - fileReader = new FileReader(file.getAbsolutePath()); - bufferedReader = new BufferedReader(fileReader); - - String line = ""; - - while ((line = bufferedReader.readLine()) != null) { - output.append(line + "\n"); - } - - response = output.toString(); - bufferedReader.close(); - - JSONObject messageDetails = new JSONObject(response); - Boolean isUserExisting = messageDetails.has(target); - - if (isUserExisting) { - JSONArray userMessages = (JSONArray) messageDetails.get(target); - userMessages.put(id); - } else { - JSONArray newUserMessages = new JSONArray(); - newUserMessages.put(id); - messageDetails.put(target, newUserMessages); - } - - fileWriter = new FileWriter(file.getAbsoluteFile()); - BufferedWriter bw = new BufferedWriter(fileWriter); - bw.write(messageDetails.toString()); - bw.close(); - } catch (IOException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } } } catch (JSONException e) { e.printStackTrace(); @@ -182,6 +124,62 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } } + private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, String id){ + File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); + FileReader fileReader = null; + FileWriter fileWriter = null; + BufferedReader bufferedReader = null; + BufferedWriter bufferedWriter = null; + String response = null; + + // create file if does nto exist + if (!file.exists()) { + try { + file.createNewFile(); + fileWriter = new FileWriter(file.getAbsoluteFile()); + bufferedWriter = new BufferedWriter(fileWriter); + bufferedWriter.write("{}"); + bufferedWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + try { + // read file into string + StringBuffer output = new StringBuffer(); + fileReader = new FileReader(file.getAbsolutePath()); + bufferedReader = new BufferedReader(fileReader); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + output.append(line + "\n"); + } + response = output.toString(); + bufferedReader.close(); + // parse file content + JSONObject messageDetails = new JSONObject(response); + // put notification id + Boolean isConversationExisting = messageDetails.has(target); + if (isConversationExisting) { + JSONArray userMessages = (JSONArray) messageDetails.get(target); + userMessages.put(id); + } else { + JSONArray newUserMessages = new JSONArray(); + newUserMessages.put(id); + messageDetails.put(target, newUserMessages); + } + // save file + fileWriter = new FileWriter(file.getAbsoluteFile()); + BufferedWriter bw = new BufferedWriter(fileWriter); + bw.write(messageDetails.toString()); + bw.close(); + } catch (IOException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + } + public static void displayNotification(Context activityOrServiceContext, Context appContext, String id, String target, String name, String groupName, String message, String eventType, String nsound, boolean showNotification, String sound, String lights) { Log.d(TAG, "displayNotification: id: " + id); Log.d(TAG, "displayNotification: Target: " + target); @@ -384,6 +382,8 @@ public static void displayNotification(Context activityOrServiceContext, Context } notificationManager.notify(notificationId, notification); + + saveNotificationsIdInFile(target, id); } From d1d935985d498abb0481363126520a6c533b186d Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 27 Nov 2018 16:32:32 +0200 Subject: [PATCH 04/24] fixes to context --- src/android/FirebasePluginMessagingService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index fc347c24b..859100d6f 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -383,7 +383,7 @@ public static void displayNotification(Context activityOrServiceContext, Context notificationManager.notify(notificationId, notification); - saveNotificationsIdInFile(target, id); + saveNotificationsIdInFile(activityOrServiceContext, target, id); } From a453dfe2559b2077b74d34f7bdd080765d42a13a Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Tue, 27 Nov 2018 16:53:50 +0200 Subject: [PATCH 05/24] Added grouping notification messages by title --- .../FirebasePluginMessagingService.java | 207 +++++++++++------- 1 file changed, 127 insertions(+), 80 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index f06ac0bc2..eced20fbb 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -1,24 +1,24 @@ package org.apache.cordova.firebase; +import android.app.Notification; import android.app.NotificationChannel; - import android.app.NotificationManager; import android.app.PendingIntent; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.preference.PreferenceManager; +import android.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.service.notification.StatusBarNotification; import android.support.v4.app.NotificationCompat; import android.support.v4.app.RemoteInput; -import android.util.Log; -import android.app.Notification; import android.text.TextUtils; -import android.content.ContentResolver; -import android.graphics.Color; +import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; @@ -34,10 +34,13 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; - +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Random; +import de.appplant.cordova.plugin.notification.Manager; + public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebasePlugin"; @@ -49,11 +52,14 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String VNC_PEER_JID = "vncPeerJid"; private static final String NOTIFY_ID = "id"; + private static final String PREVIOUS_MESSAGES_KEY = "previous_messages"; + private static final String NOTIFY_ID_FOR_DELETING = "notif_id_for_deleting"; + private static String getStringResource(Context activityOrServiceContext, String name) { return activityOrServiceContext.getString( - activityOrServiceContext.getResources().getIdentifier( - name, "string", activityOrServiceContext.getPackageName() - ) + activityOrServiceContext.getResources().getIdentifier( + name, "string", activityOrServiceContext.getPackageName() + ) ); } @@ -116,7 +122,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { data = new JSONArray(payload.get("vnc")); if (data == null || data.length() == 0) { - Log.d(TAG, "received empty data?"); + Log.d(TAG, "received empty data?"); return; } @@ -133,7 +139,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { String nsound = notification.nsound; if (TextUtils.isEmpty(target) || TextUtils.isEmpty(username)) { - Log.d(TAG, "returning due to empty 'target' or 'username' values"); + Log.d(TAG, "returning due to empty 'target' or 'username' values"); return; } @@ -183,19 +189,19 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } public static void displayNotification(Context activityOrServiceContext, Context appContext, String id, String target, String name, String groupName, String message, String eventType, String nsound, boolean showNotification, String sound, String lights) { - Log.d(TAG, "displayNotification: id: " + id); - Log.d(TAG, "displayNotification: Target: " + target); - Log.d(TAG, "displayNotification: username: " + name); - Log.d(TAG, "displayNotification: groupName: " + groupName); - Log.d(TAG, "displayNotification: message: " + message); - Log.d(TAG, "displayNotification: eventType: " + eventType); - Log.d(TAG, "displayNotification: nsound: " + nsound); - Log.d(TAG, "displayNotification: showNotification: " + showNotification); - Log.d(TAG, "displayNotification: sound: " + sound); - Log.d(TAG, "displayNotification: lights: " + lights); + Log.i(TAG, "displayNotification: id: " + id); + Log.i(TAG, "displayNotification: Target: " + target); + Log.i(TAG, "displayNotification: username: " + name); + Log.i(TAG, "displayNotification: groupName: " + groupName); + Log.i(TAG, "displayNotification: message: " + message); + Log.i(TAG, "displayNotification: eventType: " + eventType); + Log.i(TAG, "displayNotification: nsound: " + nsound); + Log.i(TAG, "displayNotification: showNotification: " + showNotification); + Log.i(TAG, "displayNotification: sound: " + sound); + Log.i(TAG, "displayNotification: lights: " + lights); if (!showNotification) { - return; + return; } Integer notificationId = Integer.parseInt(id); @@ -210,42 +216,42 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.i("VNC", "Create notification actions (>=N), NOTIFY_ID: " + id); replyPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(inlineReplyActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(inlineReplyActionName), + 0); markAsReadPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(markAsReadActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(markAsReadActionName), + 0); } else { Log.i("VNC", "Create notification actions, NOTIFY_ID: " + id); replyPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(inlineReplyActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(inlineReplyActionName), + 0); markAsReadPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(markAsReadActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(markAsReadActionName), + 0); } NotificationCompat.Action actionReply = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) - .addRemoteInput(new RemoteInput.Builder("Reply") - .setLabel("Type your message").build()) - .setAllowGeneratedReplies(true) - .build(); + android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) + .addRemoteInput(new RemoteInput.Builder("Reply") + .setLabel("Type your message").build()) + .setAllowGeneratedReplies(true) + .build(); NotificationCompat.Action actionMarkAsRead = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) - .build(); + android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) + .build(); Log.d(TAG, "going to show notification with " + nsound); @@ -272,47 +278,82 @@ public static void displayNotification(Context activityOrServiceContext, Context if (eventType.equals("chat")) { title = name; text = message; + } else { title = groupName != null && groupName.length() > 0 ? groupName : target; text = name; - if(message != null && message.trim().length() > 0) { + if (message != null && message.trim().length() > 0) { text = text + " : " + message; } } Log.d(TAG, "Notification group name: " + title); + ///////////////////// + + StatusBarNotification[] activeToasts = ((NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications(); + List msgs = new ArrayList(); + int count = 0; + int notifId = 0; + for (StatusBarNotification sbn : activeToasts) { + count++; + String currentTitle = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString(); + String currentText = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString(); + List previousMessages = sbn.getNotification().extras.getStringArrayList(PREVIOUS_MESSAGES_KEY); + Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentText + " : " + previousMessages); + + if (currentTitle.equals(title)) { + notifId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_DELETING); + msgs.addAll(previousMessages); + break; + } + } + msgs.add(text); + + ///////////////////// + // deleting previous notification + NotificationManager nMgr = (NotificationManager) activityOrServiceContext.getSystemService(Context.NOTIFICATION_SERVICE); + nMgr.cancel(notifId); + ///////////////////// + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(activityOrServiceContext, channelId); + NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null); + messagingStyle.setConversationTitle(title); + + for (String msg : msgs) { + messagingStyle.addMessage(msg, System.currentTimeMillis(), null); + } + if (nsound.equals("mute")) { Log.d(TAG, "notification nsound: " + title); - notificationBuilder - .setDefaults(NotificationCompat.DEFAULT_VIBRATE) - .setContentTitle(title) - .setContentText(text) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setStyle(new NotificationCompat.BigTextStyle().bigText(text)) - .setAutoCancel(true) - .setShowWhen(true) - .setContentIntent(pendingIntent) - .setSound(null) - .setGroup(title) - .setPriority(NotificationCompat.PRIORITY_MAX); + .setDefaults(NotificationCompat.DEFAULT_VIBRATE) + .setContentTitle(title) + .setContentText(text) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + //.setStyle(new NotificationCompat.BigTextStyle().bigText(text)) + .setStyle(messagingStyle) + .setAutoCancel(true) + .setShowWhen(true) + .setContentIntent(pendingIntent) + .setSound(null) + .setGroup(title) + .setPriority(NotificationCompat.PRIORITY_MAX); } else { notificationBuilder - .setDefaults(NotificationCompat.DEFAULT_ALL) - .setContentTitle(title) - .setContentText(text) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setStyle(new NotificationCompat.BigTextStyle().bigText(text)) - .setAutoCancel(true) - .setShowWhen(true) - .setContentIntent(pendingIntent) - .setSound(defaultSoundUri) - .setGroup(title) - .setPriority(NotificationCompat.PRIORITY_MAX); + .setDefaults(NotificationCompat.DEFAULT_ALL) + .setContentTitle(title) + .setContentText(text) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setStyle(messagingStyle) + .setAutoCancel(true) + .setShowWhen(true) + .setContentIntent(pendingIntent) + .setSound(defaultSoundUri) + .setGroup(title) + .setPriority(NotificationCompat.PRIORITY_MAX); } if (target != null && target.trim().length() > 0 && target.indexOf("@") != -1) { @@ -360,10 +401,14 @@ public static void displayNotification(Context activityOrServiceContext, Context } Notification notification = notificationBuilder.build(); + + notification.extras.putStringArrayList(PREVIOUS_MESSAGES_KEY, (ArrayList) msgs); + notification.extras.putInt(NOTIFY_ID_FOR_DELETING, notificationId); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { int iconID = android.R.id.icon; int notiID = activityOrServiceContext.getResources().getIdentifier("icon" + - "", "mipmap", activityOrServiceContext.getPackageName()); + "", "mipmap", activityOrServiceContext.getPackageName()); if (notification.contentView != null) { notification.contentView.setImageViewResource(iconID, notiID); } @@ -391,14 +436,16 @@ private String getPreference(Context context, String key) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); return settings.getString(key, null); } -} -class Payload { - public String jid; - public String name; - public String eType; - public String body; - public String gt; - public String nType; - public String nsound; + class Payload { + public String jid; + public String name; + public String eType; + public String body; + public String gt; + public String nType; + public String nsound; + } } + + From bfdb1c786c36612f687b6caf376fd3bbea6f242c Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 27 Nov 2018 17:59:45 +0200 Subject: [PATCH 06/24] hide other notifications for target when mark as read --- .../FirebasePluginMessagingService.java | 96 +++++++++++++++---- src/android/HttpPost.java | 11 +++ src/android/NotificationReceiver.java | 12 +-- 3 files changed, 97 insertions(+), 22 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 859100d6f..372a35f31 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -37,6 +37,7 @@ import java.util.Map; import java.util.Random; +import java.util.ArrayList; public class FirebasePluginMessagingService extends FirebaseMessagingService { @@ -126,18 +127,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) { private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, String id){ File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); - FileReader fileReader = null; - FileWriter fileWriter = null; - BufferedReader bufferedReader = null; - BufferedWriter bufferedWriter = null; - String response = null; + + FileWriter fileWriter; // create file if does nto exist if (!file.exists()) { try { file.createNewFile(); fileWriter = new FileWriter(file.getAbsoluteFile()); - bufferedWriter = new BufferedWriter(fileWriter); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write("{}"); bufferedWriter.close(); } catch (IOException e) { @@ -147,18 +145,15 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, try { // read file into string - StringBuffer output = new StringBuffer(); - fileReader = new FileReader(file.getAbsolutePath()); - bufferedReader = new BufferedReader(fileReader); - String line = ""; - while ((line = bufferedReader.readLine()) != null) { - output.append(line + "\n"); + String response = readNotificationsFile(file); + if(response == null){ + return; } - response = output.toString(); - bufferedReader.close(); - // parse file content + + // parse file content JSONObject messageDetails = new JSONObject(response); - // put notification id + + // put notification id Boolean isConversationExisting = messageDetails.has(target); if (isConversationExisting) { JSONArray userMessages = (JSONArray) messageDetails.get(target); @@ -180,6 +175,75 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, } } + public static ArrayList removeFromFileAndHideNotificationsForTarget(Context activityOrServiceContext, String target){ + File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); + + // create file if does nto exist + if (!file.exists()) { + return; + } + + ArrayList nIds = null; + + try { + // read file into string + String response = readNotificationsFile(file); + if(response == null){ + return; + } + + // parse file content + JSONObject messageDetails = new JSONObject(response); + + // remove notification ids + Boolean isConversationExisting = messageDetails.has(target); + if (isConversationExisting) { + nIds = new ArrayList(); + + // collect notifications ids + JSONArray userMessages = (JSONArray) messageDetails.get(target); + for (int i=0; i nIds = FirebasePluginMessagingService.removeFromFileAndHideNotificationsForTarget(context, sender); + if(nIds != null){ + for (int i=0; i 0) { - Thread thread = new Thread(new HttpPost(message.toString(), sender, notificationId, context)); + Thread thread = new Thread(new HttpPost(message.toString(), target, notificationId, context)); thread.start(); } }else if (intent.getAction().contains(MARK_AS_READ_REPLY)) { String[] actionParts = intent.getAction().split("@@"); int notificationId = Integer.parseInt(actionParts[1]); - String sender = actionParts[2]; + String target = actionParts[2]; - Log.i("VNC", "NotificationReceiver onReceive MarkAsReadReply, notificationId: " + notificationId + ", sender: " + sender); + Log.i("VNC", "NotificationReceiver onReceive MarkAsReadReply, notificationId: " + notificationId + ", target: " + target); - Thread thread = new Thread(new HttpPost(sender, notificationId, context)); + Thread thread = new Thread(new HttpPost(target, notificationId, context)); thread.start(); } } From 7f0df67f449d9a774aa9546a03d9001e32f57aaf Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 27 Nov 2018 18:07:31 +0200 Subject: [PATCH 07/24] some import fixes --- src/android/FirebasePluginMessagingService.java | 4 ++-- src/android/HttpPost.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 372a35f31..41dec899b 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -180,7 +180,7 @@ public static ArrayList removeFromFileAndHideNotificationsForTarget(Cont // create file if does nto exist if (!file.exists()) { - return; + return null; } ArrayList nIds = null; @@ -189,7 +189,7 @@ public static ArrayList removeFromFileAndHideNotificationsForTarget(Cont // read file into string String response = readNotificationsFile(file); if(response == null){ - return; + return null; } // parse file content diff --git a/src/android/HttpPost.java b/src/android/HttpPost.java index eda890485..ce0f5141f 100644 --- a/src/android/HttpPost.java +++ b/src/android/HttpPost.java @@ -145,8 +145,8 @@ public void run() { // hide all other notifications for this target ArrayList nIds = FirebasePluginMessagingService.removeFromFileAndHideNotificationsForTarget(context, sender); if(nIds != null){ - for (int i=0; i Date: Wed, 28 Nov 2018 14:08:28 +0200 Subject: [PATCH 08/24] automatically update previous notification text; --- .../FirebasePluginMessagingService.java | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 4f4616a52..703cc09f7 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -131,7 +131,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } } - private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, String id){ + private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, Integer nId){ File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); FileWriter fileWriter; @@ -156,6 +156,8 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, return; } + String nIdString = String.valueOf(nId); + // parse file content JSONObject messageDetails = new JSONObject(response); @@ -163,10 +165,20 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, Boolean isConversationExisting = messageDetails.has(target); if (isConversationExisting) { JSONArray userMessages = (JSONArray) messageDetails.get(target); - userMessages.put(id); + + boolean idAlreadyExists = false; + for (int i = 0; i < userMessages.length(); i++) { + if (userMessages.get(i).toString().equals(nIdString)){ + idAlreadyExists = true; + break; + } + } + if(!idAlreadyExists){ + userMessages.put(nIdString); + } } else { JSONArray newUserMessages = new JSONArray(); - newUserMessages.put(id); + newUserMessages.put(nIdString); messageDetails.put(target, newUserMessages); } // save file @@ -317,14 +329,6 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.d(TAG, "going to show notification with " + nsound); - Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); - Bundle bundle = new Bundle(); - bundle.putString(VNC_PEER_JID, target); - bundle.putString("vncEventType", "chat"); - bundle.putInt(NOTIFY_ID, notificationId); - intent.putExtras(bundle); - PendingIntent pendingIntent = PendingIntent.getBroadcast(activityOrServiceContext, Integer.parseInt(id), intent, PendingIntent.FLAG_UPDATE_CURRENT); - String channelId = getStringResource(activityOrServiceContext, "default_notification_channel_id"); String channelName = getStringResource(activityOrServiceContext, "default_notification_channel_name"); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); @@ -356,7 +360,6 @@ public static void displayNotification(Context activityOrServiceContext, Context StatusBarNotification[] activeToasts = ((NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications(); List msgs = new ArrayList(); int count = 0; - int notifId = 0; for (StatusBarNotification sbn : activeToasts) { count++; String currentTitle = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString(); @@ -365,19 +368,13 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentText + " : " + previousMessages); if (currentTitle.equals(title)) { - notifId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_DELETING); + notificationId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_DELETING); msgs.addAll(previousMessages); break; } } msgs.add(text); - ///////////////////// - // deleting previous notification - NotificationManager nMgr = (NotificationManager) activityOrServiceContext.getSystemService(Context.NOTIFICATION_SERVICE); - nMgr.cancel(notifId); - ///////////////////// - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(activityOrServiceContext, channelId); NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null); @@ -387,6 +384,14 @@ public static void displayNotification(Context activityOrServiceContext, Context messagingStyle.addMessage(msg, System.currentTimeMillis(), null); } + Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); + Bundle bundle = new Bundle(); + bundle.putString(VNC_PEER_JID, target); + bundle.putString("vncEventType", "chat"); + bundle.putInt(NOTIFY_ID, notificationId); + intent.putExtras(bundle); + PendingIntent pendingIntent = PendingIntent.getBroadcast(activityOrServiceContext, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); + if (nsound.equals("mute")) { Log.d(TAG, "notification nsound: " + title); notificationBuilder @@ -492,7 +497,7 @@ public static void displayNotification(Context activityOrServiceContext, Context notificationManager.notify(notificationId, notification); - saveNotificationsIdInFile(activityOrServiceContext, target, id); + saveNotificationsIdInFile(activityOrServiceContext, target, notificationId); } @@ -511,5 +516,3 @@ class Payload { public String nsound; } } - - From 9754238d722d360fdba6a2a538979633104f597a Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Wed, 28 Nov 2018 16:20:13 +0200 Subject: [PATCH 09/24] Change tag of notification defenition to "target" --- .../FirebasePluginMessagingService.java | 266 +++++++++--------- 1 file changed, 135 insertions(+), 131 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 703cc09f7..5b2eeb85c 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -38,9 +38,6 @@ import java.util.List; import java.util.Map; import java.util.Random; -import java.util.ArrayList; - -import de.appplant.cordova.plugin.notification.Manager; public class FirebasePluginMessagingService extends FirebaseMessagingService { @@ -53,14 +50,15 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String VNC_PEER_JID = "vncPeerJid"; private static final String NOTIFY_ID = "id"; - private static final String PREVIOUS_MESSAGES_KEY = "previous_messages"; - private static final String NOTIFY_ID_FOR_DELETING = "notif_id_for_deleting"; - + private static final String PREVIOUS_MESSAGES = "previousMessages"; + private static final String NOTIFY_ID_FOR_UPDATING = "notifIdForUpdating"; + private static final String MESSAGE_TARGET = "messageTarget"; + private static String getStringResource(Context activityOrServiceContext, String name) { return activityOrServiceContext.getString( - activityOrServiceContext.getResources().getIdentifier( - name, "string", activityOrServiceContext.getPackageName() - ) + activityOrServiceContext.getResources().getIdentifier( + name, "string", activityOrServiceContext.getPackageName() + ) ); } @@ -131,7 +129,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } } - private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, Integer nId){ + private static void saveNotificationsIdInFile(Context activityOrServiceContext, String target, Integer nId) { File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); FileWriter fileWriter; @@ -152,8 +150,8 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, try { // read file into string String response = readNotificationsFile(file); - if(response == null){ - return; + if (response == null) { + return; } String nIdString = String.valueOf(nId); @@ -168,12 +166,12 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, boolean idAlreadyExists = false; for (int i = 0; i < userMessages.length(); i++) { - if (userMessages.get(i).toString().equals(nIdString)){ - idAlreadyExists = true; - break; - } + if (userMessages.get(i).toString().equals(nIdString)) { + idAlreadyExists = true; + break; + } } - if(!idAlreadyExists){ + if (!idAlreadyExists) { userMessages.put(nIdString); } } else { @@ -181,7 +179,7 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, newUserMessages.put(nIdString); messageDetails.put(target, newUserMessages); } - // save file + // save file fileWriter = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fileWriter); bw.write(messageDetails.toString()); @@ -193,68 +191,68 @@ private static void saveNotificationsIdInFile(Context activityOrServiceContext, } } - public static ArrayList removeFromFileAndHideNotificationsForTarget(Context activityOrServiceContext, String target){ - File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); + public static ArrayList removeFromFileAndHideNotificationsForTarget(Context activityOrServiceContext, String target) { + File file = new File(activityOrServiceContext.getFilesDir(), FILE_NAME); - // create file if does nto exist - if (!file.exists()) { - return null; - } + // create file if does nto exist + if (!file.exists()) { + return null; + } - ArrayList nIds = null; + ArrayList nIds = null; - try { - // read file into string - String response = readNotificationsFile(file); - if(response == null){ - return null; - } - - // parse file content - JSONObject messageDetails = new JSONObject(response); - - // remove notification ids - Boolean isConversationExisting = messageDetails.has(target); - if (isConversationExisting) { - nIds = new ArrayList(); - - // collect notifications ids - JSONArray userMessages = (JSONArray) messageDetails.get(target); - for (int i=0; i(); + + // collect notifications ids + JSONArray userMessages = (JSONArray) messageDetails.get(target); + for (int i = 0; i < userMessages.length(); i++) { + nIds.add(userMessages.getString(i)); + } + + // remove + messageDetails.remove(target); + + // save file + FileWriter fileWriter = new FileWriter(file.getAbsoluteFile()); + BufferedWriter bw = new BufferedWriter(fileWriter); + bw.write(messageDetails.toString()); + bw.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + + return nIds; } - private static String readNotificationsFile(File file){ + private static String readNotificationsFile(File file) { String response = null; try { - // read file into string - StringBuffer output = new StringBuffer(); - FileReader fileReader = new FileReader(file.getAbsolutePath()); - BufferedReader bufferedReader = new BufferedReader(fileReader); - String line = ""; - while ((line = bufferedReader.readLine()) != null) { - output.append(line + "\n"); - } - response = output.toString(); - bufferedReader.close(); + // read file into string + StringBuffer output = new StringBuffer(); + FileReader fileReader = new FileReader(file.getAbsolutePath()); + BufferedReader bufferedReader = new BufferedReader(fileReader); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + output.append(line + "\n"); + } + response = output.toString(); + bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } @@ -290,42 +288,42 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.i("VNC", "Create notification actions (>=N), NOTIFY_ID: " + id); replyPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(inlineReplyActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(inlineReplyActionName), + 0); markAsReadPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(markAsReadActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(markAsReadActionName), + 0); } else { Log.i("VNC", "Create notification actions, NOTIFY_ID: " + id); replyPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(inlineReplyActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(inlineReplyActionName), + 0); markAsReadPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(markAsReadActionName), - 0); + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(markAsReadActionName), + 0); } NotificationCompat.Action actionReply = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) - .addRemoteInput(new RemoteInput.Builder("Reply") - .setLabel("Type your message").build()) - .setAllowGeneratedReplies(true) - .build(); + android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) + .addRemoteInput(new RemoteInput.Builder("Reply") + .setLabel("Type your message").build()) + .setAllowGeneratedReplies(true) + .build(); NotificationCompat.Action actionMarkAsRead = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) - .build(); + android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) + .build(); Log.d(TAG, "going to show notification with " + nsound); @@ -355,20 +353,26 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.d(TAG, "Notification group name: " + title); - ///////////////////// - + //////////////////////////////////////////////////////////////////////////////////// + // Find previous messages and update notification ID + //////////////////////////////////////////////////////////////////////////////////// StatusBarNotification[] activeToasts = ((NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications(); List msgs = new ArrayList(); int count = 0; for (StatusBarNotification sbn : activeToasts) { count++; - String currentTitle = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString(); - String currentText = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString(); - List previousMessages = sbn.getNotification().extras.getStringArrayList(PREVIOUS_MESSAGES_KEY); - Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentText + " : " + previousMessages); - - if (currentTitle.equals(title)) { - notificationId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_DELETING); + Notification curNotif = sbn.getNotification(); + Bundle bundle = curNotif.extras; + String currentTitle = bundle.getCharSequence(Notification.EXTRA_TITLE).toString(); + String currentText = bundle.getCharSequence(Notification.EXTRA_TEXT).toString(); + String currentTarget = bundle.getString(MESSAGE_TARGET); + List previousMessages = sbn.getNotification().extras.getStringArrayList(PREVIOUS_MESSAGES); + + Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentTarget + " : " + + currentText + " : " + previousMessages); + + if (currentTarget.equals(target)) { + notificationId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_UPDATING); msgs.addAll(previousMessages); break; } @@ -395,32 +399,31 @@ public static void displayNotification(Context activityOrServiceContext, Context if (nsound.equals("mute")) { Log.d(TAG, "notification nsound: " + title); notificationBuilder - .setDefaults(NotificationCompat.DEFAULT_VIBRATE) - .setContentTitle(title) - .setContentText(text) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - //.setStyle(new NotificationCompat.BigTextStyle().bigText(text)) - .setStyle(messagingStyle) - .setAutoCancel(true) - .setShowWhen(true) - .setContentIntent(pendingIntent) - .setSound(null) - .setGroup(title) - .setPriority(NotificationCompat.PRIORITY_MAX); + .setDefaults(NotificationCompat.DEFAULT_VIBRATE) + .setContentTitle(title) + .setContentText(text) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setStyle(messagingStyle) + .setAutoCancel(true) + .setShowWhen(true) + .setContentIntent(pendingIntent) + .setSound(null) + .setGroup(title) + .setPriority(NotificationCompat.PRIORITY_MAX); } else { notificationBuilder - .setDefaults(NotificationCompat.DEFAULT_ALL) - .setContentTitle(title) - .setContentText(text) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setStyle(messagingStyle) - .setAutoCancel(true) - .setShowWhen(true) - .setContentIntent(pendingIntent) - .setSound(defaultSoundUri) - .setGroup(title) - .setPriority(NotificationCompat.PRIORITY_MAX); + .setDefaults(NotificationCompat.DEFAULT_ALL) + .setContentTitle(title) + .setContentText(text) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setStyle(messagingStyle) + .setAutoCancel(true) + .setShowWhen(true) + .setContentIntent(pendingIntent) + .setSound(defaultSoundUri) + .setGroup(title) + .setPriority(NotificationCompat.PRIORITY_MAX); } if (target != null && target.trim().length() > 0 && target.indexOf("@") != -1) { @@ -469,13 +472,14 @@ public static void displayNotification(Context activityOrServiceContext, Context Notification notification = notificationBuilder.build(); - notification.extras.putStringArrayList(PREVIOUS_MESSAGES_KEY, (ArrayList) msgs); - notification.extras.putInt(NOTIFY_ID_FOR_DELETING, notificationId); + notification.extras.putStringArrayList(PREVIOUS_MESSAGES, (ArrayList) msgs); + notification.extras.putInt(NOTIFY_ID_FOR_UPDATING, notificationId); + notification.extras.putString(MESSAGE_TARGET, target); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { int iconID = android.R.id.icon; int notiID = activityOrServiceContext.getResources().getIdentifier("icon" + - "", "mipmap", activityOrServiceContext.getPackageName()); + "", "mipmap", activityOrServiceContext.getPackageName()); if (notification.contentView != null) { notification.contentView.setImageViewResource(iconID, notiID); } From a7c3bf0de8d956d6571e35430d1c58c7b783a561 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 28 Nov 2018 21:23:55 +0200 Subject: [PATCH 10/24] fix NPE --- src/android/FirebasePluginMessagingService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 5b2eeb85c..40502e0a9 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -53,7 +53,7 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String PREVIOUS_MESSAGES = "previousMessages"; private static final String NOTIFY_ID_FOR_UPDATING = "notifIdForUpdating"; private static final String MESSAGE_TARGET = "messageTarget"; - + private static String getStringResource(Context activityOrServiceContext, String name) { return activityOrServiceContext.getString( activityOrServiceContext.getResources().getIdentifier( @@ -371,7 +371,7 @@ public static void displayNotification(Context activityOrServiceContext, Context Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentTarget + " : " + currentText + " : " + previousMessages); - if (currentTarget.equals(target)) { + if (currentTarget != null && currentTarget.equals(target)) { notificationId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_UPDATING); msgs.addAll(previousMessages); break; From 1d9bb574e351c5a8512c93a5f9d74b329e4015c5 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 28 Nov 2018 21:44:07 +0200 Subject: [PATCH 11/24] fixes for inline reply action (not worked in groups) --- .../FirebasePluginMessagingService.java | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 40502e0a9..ea650130e 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -278,55 +278,6 @@ public static void displayNotification(Context activityOrServiceContext, Context Integer notificationId = Integer.parseInt(id); - String inlineReplyActionName = NOTIFICATION_REPLY + "@@" + id + "@@" + target; - String markAsReadActionName = MARK_AS_READ_REPLY + "@@" + id + "@@" + target; - // - PendingIntent replyPendingIntent; - PendingIntent markAsReadPendingIntent; - // - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - Log.i("VNC", "Create notification actions (>=N), NOTIFY_ID: " + id); - - replyPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(inlineReplyActionName), - 0); - - markAsReadPendingIntent = PendingIntent.getBroadcast(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, NotificationReceiver.class) - .setAction(markAsReadActionName), - 0); - } else { - Log.i("VNC", "Create notification actions, NOTIFY_ID: " + id); - - replyPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(inlineReplyActionName), - 0); - - markAsReadPendingIntent = PendingIntent.getActivity(appContext, - REQUEST_CODE_HELP, - new Intent(activityOrServiceContext, ReplyActivity.class) - .setAction(markAsReadActionName), - 0); - } - - NotificationCompat.Action actionReply = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) - .addRemoteInput(new RemoteInput.Builder("Reply") - .setLabel("Type your message").build()) - .setAllowGeneratedReplies(true) - .build(); - - NotificationCompat.Action actionMarkAsRead = new NotificationCompat.Action.Builder( - android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) - .build(); - - Log.d(TAG, "going to show notification with " + nsound); - String channelId = getStringResource(activityOrServiceContext, "default_notification_channel_id"); String channelName = getStringResource(activityOrServiceContext, "default_notification_channel_name"); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); @@ -342,7 +293,6 @@ public static void displayNotification(Context activityOrServiceContext, Context if (eventType.equals("chat")) { title = name; text = message; - } else { title = groupName != null && groupName.length() > 0 ? groupName : target; text = name; @@ -351,7 +301,7 @@ public static void displayNotification(Context activityOrServiceContext, Context } } - Log.d(TAG, "Notification group name: " + title); + Log.d(TAG, "Notification title: " + title); //////////////////////////////////////////////////////////////////////////////////// // Find previous messages and update notification ID @@ -426,6 +376,56 @@ public static void displayNotification(Context activityOrServiceContext, Context .setPriority(NotificationCompat.PRIORITY_MAX); } + // Add actions + // + String notificationIdString = String.valueOf(notificationId); + String inlineReplyActionName = NOTIFICATION_REPLY + "@@" + notificationIdString + "@@" + target; + String markAsReadActionName = MARK_AS_READ_REPLY + "@@" + notificationIdString + "@@" + target; + // + PendingIntent replyPendingIntent; + PendingIntent markAsReadPendingIntent; + // + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + Log.i("VNC", "Create notification actions (>=N), NOTIFY_ID: " + id); + + replyPendingIntent = PendingIntent.getBroadcast(appContext, + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(inlineReplyActionName), + 0); + + markAsReadPendingIntent = PendingIntent.getBroadcast(appContext, + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, NotificationReceiver.class) + .setAction(markAsReadActionName), + 0); + } else { + Log.i("VNC", "Create notification actions, NOTIFY_ID: " + id); + + replyPendingIntent = PendingIntent.getActivity(appContext, + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(inlineReplyActionName), + 0); + + markAsReadPendingIntent = PendingIntent.getActivity(appContext, + REQUEST_CODE_HELP, + new Intent(activityOrServiceContext, ReplyActivity.class) + .setAction(markAsReadActionName), + 0); + } + + NotificationCompat.Action actionReply = new NotificationCompat.Action.Builder( + android.R.drawable.ic_menu_revert, "Reply", replyPendingIntent) + .addRemoteInput(new RemoteInput.Builder("Reply") + .setLabel("Type your message").build()) + .setAllowGeneratedReplies(true) + .build(); + + NotificationCompat.Action actionMarkAsRead = new NotificationCompat.Action.Builder( + android.R.drawable.ic_menu_revert, "Mark as read", markAsReadPendingIntent) + .build(); + if (target != null && target.trim().length() > 0 && target.indexOf("@") != -1) { notificationBuilder.addAction(actionReply); notificationBuilder.addAction(actionMarkAsRead); From 40536383def6e24d3e5fdccf7c8cc494488e87b5 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 28 Nov 2018 22:32:28 +0200 Subject: [PATCH 12/24] set notification id as hashcode from target --- .../FirebasePluginMessagingService.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index ea650130e..c802b578f 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -105,9 +105,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) { for (int i = 0; i < data.length(); i++) { Payload notification = new Gson().fromJson(data.get(i).toString(), Payload.class); - Random rand = new Random(); - int n = rand.nextInt(1000) + 1; - String id = Integer.toString(n); String target = notification.jid; String username = notification.name; String groupName = notification.gt; @@ -121,7 +118,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()); - displayNotification(this, getApplicationContext(), id, target, username, groupName, message, eventType, nsound, showNotification, "", ""); + displayNotification(this, getApplicationContext(), 0, target, username, groupName, message, eventType, nsound, showNotification, "", ""); } } catch (JSONException e) { e.printStackTrace(); @@ -261,7 +258,6 @@ private static String readNotificationsFile(File file) { } public static void displayNotification(Context activityOrServiceContext, Context appContext, String id, String target, String name, String groupName, String message, String eventType, String nsound, boolean showNotification, String sound, String lights) { - Log.i(TAG, "displayNotification: id: " + id); Log.i(TAG, "displayNotification: Target: " + target); Log.i(TAG, "displayNotification: username: " + name); Log.i(TAG, "displayNotification: groupName: " + groupName); @@ -276,7 +272,12 @@ public static void displayNotification(Context activityOrServiceContext, Context return; } - Integer notificationId = Integer.parseInt(id); + Integer notificationId = Integer.valueOf(id); + if(notificationId == 0){ + notificationId = target.hashCode(); + } + + Log.i(TAG, "displayNotification: id: " + notificationId); String channelId = getStringResource(activityOrServiceContext, "default_notification_channel_id"); String channelName = getStringResource(activityOrServiceContext, "default_notification_channel_name"); @@ -319,7 +320,7 @@ public static void displayNotification(Context activityOrServiceContext, Context List previousMessages = sbn.getNotification().extras.getStringArrayList(PREVIOUS_MESSAGES); Log.i("vnc", "NOTIFICATION " + count + " : = " + currentTitle + " : " + currentTarget + " : " - + currentText + " : " + previousMessages); + + currentText + " : " + previousMessages + ". Message: " + message); if (currentTarget != null && currentTarget.equals(target)) { notificationId = sbn.getNotification().extras.getInt(NOTIFY_ID_FOR_UPDATING); @@ -327,6 +328,9 @@ public static void displayNotification(Context activityOrServiceContext, Context break; } } + if(count == 0){ + Log.i("vnc", "no notifications in Status bar, when message: " + message); + } msgs.add(text); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(activityOrServiceContext, channelId); From 24eb74153c84f3762ab5b162dd38c179a73c912c Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 28 Nov 2018 22:52:38 +0200 Subject: [PATCH 13/24] type --- src/android/FirebasePluginMessagingService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index c802b578f..156acbb09 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -118,7 +118,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()); - displayNotification(this, getApplicationContext(), 0, target, username, groupName, message, eventType, nsound, showNotification, "", ""); + displayNotification(this, getApplicationContext(), "0", target, username, groupName, message, eventType, nsound, showNotification, "", ""); } } catch (JSONException e) { e.printStackTrace(); From 1257f088cc61de137fca7fa9d1f288911473e894 Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Thu, 29 Nov 2018 16:41:22 +0200 Subject: [PATCH 14/24] Fixed crash problem with StatusBarNotification for android version less then 23 --- src/android/FirebasePluginMessagingService.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 156acbb09..b5cf4c588 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -307,7 +307,10 @@ public static void displayNotification(Context activityOrServiceContext, Context //////////////////////////////////////////////////////////////////////////////////// // Find previous messages and update notification ID //////////////////////////////////////////////////////////////////////////////////// - StatusBarNotification[] activeToasts = ((NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications(); + StatusBarNotification[] activeToasts = new StatusBarNotification[0]; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { + activeToasts = ((NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications(); + } List msgs = new ArrayList(); int count = 0; for (StatusBarNotification sbn : activeToasts) { From a2fa73d7411bacfa56be663005b955bd7f4678dd Mon Sep 17 00:00:00 2001 From: Igor Date: Thu, 29 Nov 2018 22:41:55 +0200 Subject: [PATCH 15/24] added onNotificationMarkAsRead callback --- src/android/FirebasePlugin.java | 35 +++++++++++++++++++++++++++++++++ src/android/HttpPost.java | 4 ++++ www/firebase.js | 4 ++++ 3 files changed, 43 insertions(+) diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index 92e96e8af..6953ad627 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -73,6 +73,7 @@ public class FirebasePlugin extends CordovaPlugin { private static ArrayList notificationStack = null; private static CallbackContext notificationCallbackContext; private static CallbackContext tokenRefreshCallbackContext; + private static CallbackContext notificationMarkAsReadCallbackContext; @Override protected void pluginInitialize() { @@ -134,6 +135,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if (action.equals("onNotificationOpen")) { this.onNotificationOpen(callbackContext); return true; + } else if (action.equals("onNotificationMarkAsRead")) { + this.onNotificationMarkAsRead(callbackContext); + return true; } else if (action.equals("onTokenRefresh")) { this.onTokenRefresh(callbackContext); return true; @@ -235,6 +239,7 @@ public void onResume(boolean multitasking) { public void onReset() { FirebasePlugin.notificationCallbackContext = null; FirebasePlugin.tokenRefreshCallbackContext = null; + FirebasePlugin.notificationMarkAsReadCallbackContext = null; } @Override @@ -302,6 +307,10 @@ private void onNotificationOpen(final CallbackContext callbackContext) { } } + private void onNotificationMarkAsRead(final CallbackContext callbackContext) { + FirebasePlugin.notificationMarkAsReadCallbackContext = callbackContext; + } + private void onTokenRefresh(final CallbackContext callbackContext) { FirebasePlugin.tokenRefreshCallbackContext = callbackContext; @@ -354,6 +363,32 @@ public static void sendNotification(Bundle bundle, Context context) { } } + public static void sendNotificationMarkAsRead(Bundle bundle) { + final CallbackContext callbackContext = FirebasePlugin.notificationMarkAsReadCallbackContext; + + if(callbackContext == null || bundle = null){ + return; + } + + JSONObject json = new JSONObject(); + Set keys = bundle.keySet(); + for (String key : keys) { + try { + json.put(key, bundle.get(key)); + } catch (JSONException e) { + if(FirebasePlugin.crashlyticsInit()){ + Crashlytics.logException(e); + } + callbackContext.error(e.getMessage()); + return; + } + } + + PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, json); + pluginresult.setKeepCallback(true); + callbackContext.sendPluginResult(pluginresult); + } + public static void sendToken(String token) { if (FirebasePlugin.tokenRefreshCallbackContext == null) { return; diff --git a/src/android/HttpPost.java b/src/android/HttpPost.java index ce0f5141f..f8ab8155b 100644 --- a/src/android/HttpPost.java +++ b/src/android/HttpPost.java @@ -149,6 +149,10 @@ public void run() { notificationManager.cancel(Integer.parseInt(nIds.get(i))); } } + + Bundle data = new Bundle(); + // data.putBoolean("tap", true); + FirebasePlugin.sendNotificationMarkAsRead(data); } } } diff --git a/www/firebase.js b/www/firebase.js index 0d0883502..963bf7f59 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -32,6 +32,10 @@ exports.onNotificationOpen = function (success, error) { exec(success, error, "FirebasePlugin", "onNotificationOpen", []); }; +exports.onNotificationMarkAsRead = function (success, error) { + exec(success, error, "FirebasePlugin", "onNotificationMarkAsRead", []); +}; + exports.onTokenRefresh = function (success, error) { exec(success, error, "FirebasePlugin", "onTokenRefresh", []); }; From e9c85a35583db4930944fce58ae9d6fa6877e787 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 30 Nov 2018 10:08:58 +0200 Subject: [PATCH 16/24] fixes for sendNotificationMarkAsRead --- src/android/FirebasePlugin.java | 2 +- src/android/HttpPost.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index 6953ad627..e5dfbcaef 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -308,7 +308,7 @@ private void onNotificationOpen(final CallbackContext callbackContext) { } private void onNotificationMarkAsRead(final CallbackContext callbackContext) { - FirebasePlugin.notificationMarkAsReadCallbackContext = callbackContext; + FirebasePlugin.notificationMarkAsReadCallbackContext = callbackContext; } private void onTokenRefresh(final CallbackContext callbackContext) { diff --git a/src/android/HttpPost.java b/src/android/HttpPost.java index f8ab8155b..c50630704 100644 --- a/src/android/HttpPost.java +++ b/src/android/HttpPost.java @@ -151,7 +151,7 @@ public void run() { } Bundle data = new Bundle(); - // data.putBoolean("tap", true); + data.putBoolean("target", sender); FirebasePlugin.sendNotificationMarkAsRead(data); } } From b354a6350c789c8a28993078f4c7d3c7e52900f7 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 30 Nov 2018 12:59:05 +0200 Subject: [PATCH 17/24] cleanup --- src/android/FirebasePlugin.java | 6 +++++- src/android/HttpPost.java | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index e5dfbcaef..32efdb78a 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -366,7 +366,7 @@ public static void sendNotification(Bundle bundle, Context context) { public static void sendNotificationMarkAsRead(Bundle bundle) { final CallbackContext callbackContext = FirebasePlugin.notificationMarkAsReadCallbackContext; - if(callbackContext == null || bundle = null){ + if(callbackContext == null || bundle == null){ return; } @@ -422,6 +422,10 @@ public static boolean hasNotificationsCallback() { return FirebasePlugin.notificationCallbackContext != null; } + public static boolean hasNotificationsMarkAsReadCallback() { + return FirebasePlugin.notificationMarkAsReadCallbackContext != null; + } + @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); diff --git a/src/android/HttpPost.java b/src/android/HttpPost.java index c50630704..c8e928eb6 100644 --- a/src/android/HttpPost.java +++ b/src/android/HttpPost.java @@ -6,7 +6,7 @@ import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.util.Log; - +import android.os.Bundle; import org.json.JSONObject; import com.google.gson.Gson; @@ -151,7 +151,7 @@ public void run() { } Bundle data = new Bundle(); - data.putBoolean("target", sender); + data.putString("target", sender); FirebasePlugin.sendNotificationMarkAsRead(data); } } From 445c8f415726519395d357079dd71369198630cd Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Fri, 30 Nov 2018 16:55:05 +0200 Subject: [PATCH 18/24] Added parser and handler for notification's texts format --- .../FirebasePluginMessagingService.java | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index b5cf4c588..4deb892e4 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -35,9 +35,9 @@ import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.Random; public class FirebasePluginMessagingService extends FirebaseMessagingService { @@ -54,6 +54,13 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String NOTIFY_ID_FOR_UPDATING = "notifIdForUpdating"; private static final String MESSAGE_TARGET = "messageTarget"; + private static final String AUDIO_FORMAT = "Audio"; + private static final String VOICE_FORMAT = "Voice Message"; + private static final String PHOTO_FORMAT = "Photo"; + private static final String LINK_FORMAT = "Link"; + private final String EMODJI_FORMAT = "Emodji"; + + private static String getStringResource(Context activityOrServiceContext, String name) { return activityOrServiceContext.getString( activityOrServiceContext.getResources().getIdentifier( @@ -273,7 +280,7 @@ public static void displayNotification(Context activityOrServiceContext, Context } Integer notificationId = Integer.valueOf(id); - if(notificationId == 0){ + if (notificationId == 0) { notificationId = target.hashCode(); } @@ -331,8 +338,8 @@ public static void displayNotification(Context activityOrServiceContext, Context break; } } - if(count == 0){ - Log.i("vnc", "no notifications in Status bar, when message: " + message); + if (count == 0) { + Log.i("vnc", "no notifications in Status bar, when message: " + message); } msgs.add(text); @@ -342,9 +349,15 @@ public static void displayNotification(Context activityOrServiceContext, Context messagingStyle.setConversationTitle(title); for (String msg : msgs) { + Log.i("vnc", "TYPE OF LINK = " + getTypeOfLink(msg)); + String typeOfLink = getTypeOfLink(msg); + if (typeOfLink != null) { + msg = typeOfLink; + } messagingStyle.addMessage(msg, System.currentTimeMillis(), null); } + Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); Bundle bundle = new Bundle(); bundle.putString(VNC_PEER_JID, target); @@ -511,6 +524,36 @@ public static void displayNotification(Context activityOrServiceContext, Context saveNotificationsIdInFile(activityOrServiceContext, target, notificationId); } + private static String getTypeOfLink(String text) { + if (text == null || text.isEmpty()) { + return null; + } + text = text.trim(); + if (!text.startsWith("http") && !text.startsWith("https") || + ((text.startsWith("http") || text.startsWith("https") && text.length() > 8))) { + return null; + } + + List photoFormat = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp"); + List audioFormat = Arrays.asList("wav", "mp3", "wma", "webm", "ogg"); + + if (text.contains("audio_recording_")) { + return VOICE_FORMAT; + } + + String extension = text.substring(text.lastIndexOf(".") + 1); + + if (photoFormat.indexOf(extension) != -1) { + return PHOTO_FORMAT; + } + + if (audioFormat.indexOf(extension) != -1) { + return AUDIO_FORMAT; + } + + return LINK_FORMAT; + } + private String getPreference(Context context, String key) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); From 0d4b03bca21be2eef7661ec5e0c8ede1f84f51af Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Fri, 30 Nov 2018 17:29:15 +0200 Subject: [PATCH 19/24] Corrected conditions for define link --- src/android/FirebasePluginMessagingService.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 4deb892e4..365a6b370 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Map; + public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebasePlugin"; @@ -58,7 +59,7 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String VOICE_FORMAT = "Voice Message"; private static final String PHOTO_FORMAT = "Photo"; private static final String LINK_FORMAT = "Link"; - private final String EMODJI_FORMAT = "Emodji"; + private final String EMODJI_FORMAT = "Emoji"; private static String getStringResource(Context activityOrServiceContext, String name) { @@ -366,14 +367,20 @@ public static void displayNotification(Context activityOrServiceContext, Context intent.putExtras(bundle); PendingIntent pendingIntent = PendingIntent.getBroadcast(activityOrServiceContext, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); + //RemoteViews contentView = new RemoteViews(activityOrServiceContext.getPackageName(), R.layout.custom_push); + //contentView.setImageViewResource(R.id.image, R.mipmap.icon); + //contentView.setTextViewText(R.id.title, "Custom notification"); + //contentView.setTextViewText(R.id.text, text); + + if (nsound.equals("mute")) { Log.d(TAG, "notification nsound: " + title); notificationBuilder .setDefaults(NotificationCompat.DEFAULT_VIBRATE) .setContentTitle(title) - .setContentText(text) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setStyle(messagingStyle) +// .setStyle(messagingStyle) .setAutoCancel(true) .setShowWhen(true) .setContentIntent(pendingIntent) @@ -529,8 +536,7 @@ private static String getTypeOfLink(String text) { return null; } text = text.trim(); - if (!text.startsWith("http") && !text.startsWith("https") || - ((text.startsWith("http") || text.startsWith("https") && text.length() > 8))) { + if (!text.startsWith("http") && !text.startsWith("https")) { return null; } From 8d217e20ffe301fe6d443499c4fb61fc07f50e8f Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Fri, 30 Nov 2018 17:34:25 +0200 Subject: [PATCH 20/24] Revert "Corrected conditions for define link" This reverts commit 0d4b03bca21be2eef7661ec5e0c8ede1f84f51af. --- src/android/FirebasePluginMessagingService.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 365a6b370..4deb892e4 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -39,7 +39,6 @@ import java.util.List; import java.util.Map; - public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String TAG = "FirebasePlugin"; @@ -59,7 +58,7 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String VOICE_FORMAT = "Voice Message"; private static final String PHOTO_FORMAT = "Photo"; private static final String LINK_FORMAT = "Link"; - private final String EMODJI_FORMAT = "Emoji"; + private final String EMODJI_FORMAT = "Emodji"; private static String getStringResource(Context activityOrServiceContext, String name) { @@ -367,20 +366,14 @@ public static void displayNotification(Context activityOrServiceContext, Context intent.putExtras(bundle); PendingIntent pendingIntent = PendingIntent.getBroadcast(activityOrServiceContext, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); - //RemoteViews contentView = new RemoteViews(activityOrServiceContext.getPackageName(), R.layout.custom_push); - //contentView.setImageViewResource(R.id.image, R.mipmap.icon); - //contentView.setTextViewText(R.id.title, "Custom notification"); - //contentView.setTextViewText(R.id.text, text); - - if (nsound.equals("mute")) { Log.d(TAG, "notification nsound: " + title); notificationBuilder .setDefaults(NotificationCompat.DEFAULT_VIBRATE) .setContentTitle(title) - + .setContentText(text) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) -// .setStyle(messagingStyle) + .setStyle(messagingStyle) .setAutoCancel(true) .setShowWhen(true) .setContentIntent(pendingIntent) @@ -536,7 +529,8 @@ private static String getTypeOfLink(String text) { return null; } text = text.trim(); - if (!text.startsWith("http") && !text.startsWith("https")) { + if (!text.startsWith("http") && !text.startsWith("https") || + ((text.startsWith("http") || text.startsWith("https") && text.length() > 8))) { return null; } From 0aeab1068a4a5f9395eae9f3c53bf0fb00665687 Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Fri, 30 Nov 2018 17:35:59 +0200 Subject: [PATCH 21/24] Fixed bugs with conditions for link defenition --- src/android/FirebasePluginMessagingService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 4deb892e4..75ad510b0 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -529,8 +529,7 @@ private static String getTypeOfLink(String text) { return null; } text = text.trim(); - if (!text.startsWith("http") && !text.startsWith("https") || - ((text.startsWith("http") || text.startsWith("https") && text.length() > 8))) { + if (!text.startsWith("http") && !text.startsWith("https")) { return null; } From 1a16bd5923c3afc5e667d16fe4949c3009b2589f Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Fri, 30 Nov 2018 18:20:29 +0200 Subject: [PATCH 22/24] Fixed msgs format for group msgs --- src/android/FirebasePluginMessagingService.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 75ad510b0..473bd418f 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -296,6 +296,9 @@ public static void displayNotification(Context activityOrServiceContext, Context defaultSoundUri = null; } + String typeOfLink = getTypeOfLink(message); + message = typeOfLink == null ? message : typeOfLink; + String title; String text; if (eventType.equals("chat")) { @@ -349,15 +352,9 @@ public static void displayNotification(Context activityOrServiceContext, Context messagingStyle.setConversationTitle(title); for (String msg : msgs) { - Log.i("vnc", "TYPE OF LINK = " + getTypeOfLink(msg)); - String typeOfLink = getTypeOfLink(msg); - if (typeOfLink != null) { - msg = typeOfLink; - } messagingStyle.addMessage(msg, System.currentTimeMillis(), null); } - Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); Bundle bundle = new Bundle(); bundle.putString(VNC_PEER_JID, target); From 83cc0f7233ffaf8a4450213cdd25f6a2759680b6 Mon Sep 17 00:00:00 2001 From: Dave Lombardo Date: Mon, 3 Dec 2018 13:41:51 +0200 Subject: [PATCH 23/24] Fixed notification title on Android9 --- src/android/FirebasePluginMessagingService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index 473bd418f..fe28ffe51 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -349,10 +349,17 @@ public static void displayNotification(Context activityOrServiceContext, Context NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(activityOrServiceContext, channelId); NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null); - messagingStyle.setConversationTitle(title); - + // + if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.O) { + messagingStyle.setConversationTitle(title); + } + // for (String msg : msgs) { - messagingStyle.addMessage(msg, System.currentTimeMillis(), null); + if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.O) { + messagingStyle.addMessage(msg, System.currentTimeMillis(), null); + }else{ + messagingStyle.addMessage(msg, System.currentTimeMillis(), title); + } } Intent intent = new Intent(activityOrServiceContext, OnNotificationOpenReceiver.class); From 241fa419cc7dd1d8ebf2c9081978f2821df6b0ed Mon Sep 17 00:00:00 2001 From: "yuriy.kaplunov" Date: Mon, 3 Dec 2018 18:35:54 +0200 Subject: [PATCH 24/24] Added special symbols for special notifications --- src/android/FirebasePluginMessagingService.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/android/FirebasePluginMessagingService.java b/src/android/FirebasePluginMessagingService.java index fe28ffe51..44766d2f7 100755 --- a/src/android/FirebasePluginMessagingService.java +++ b/src/android/FirebasePluginMessagingService.java @@ -54,12 +54,10 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService { private static final String NOTIFY_ID_FOR_UPDATING = "notifIdForUpdating"; private static final String MESSAGE_TARGET = "messageTarget"; - private static final String AUDIO_FORMAT = "Audio"; - private static final String VOICE_FORMAT = "Voice Message"; - private static final String PHOTO_FORMAT = "Photo"; - private static final String LINK_FORMAT = "Link"; - private final String EMODJI_FORMAT = "Emodji"; - + private static final String AUDIO_FORMAT = "\uD83D\uDCFC Audio"; + private static final String VOICE_FORMAT = "\uD83C\uDF99 Voice Message"; + private static final String PHOTO_FORMAT = "\uD83D\uDCF7 Photo"; + private static final String LINK_FORMAT = "\uD83D\uDD17 Link"; private static String getStringResource(Context activityOrServiceContext, String name) { return activityOrServiceContext.getString(