Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature 'Missed call' for late push notification #43

Open
wants to merge 11 commits into
base: master-universal
Choose a base branch
from
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ xmlns:android="http://schemas.android.com/apk/res/android">
<source-file src="src/android/utils/WidgetNotifier.java" target-dir="src/org/apache/cordova/firebase/utils" />
<source-file src="src/android/utils/ImagesUtils.java" target-dir="src/org/apache/cordova/firebase/utils" />
<source-file src="src/android/utils/FcmLoggerUtils.java" target-dir="src/org/apache/cordova/firebase/utils" />
<source-file src="src/android/utils/DateUtils.java" target-dir="src/org/apache/cordova/firebase/utils" />

<source-file src="src/android/notification/NotificationCreator.java" target-dir="src/org/apache/cordova/firebase/notification" />
<source-file src="src/android/notification/NotificationManager.java" target-dir="src/org/apache/cordova/firebase/notification" />
Expand Down
2 changes: 1 addition & 1 deletion src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ public void run() {
"eventType= " + eventType);

NotificationManager.displayTalkCallNotification(activityContext, appContext, msgid, eventType,
target, username, groupName, message, initiator, receiver);
target, username, groupName, message, initiator, receiver, 0);
callbackContext.success();
} catch (Exception e) {
if (FirebasePlugin.isCrashlyticsEnabled()) {
Expand Down
3 changes: 2 additions & 1 deletion src/android/PayloadProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void processTalkPayload(Map<String, String> payload) {
final String eventType = notification.eType;
final String nsound = notification.nsound;
final String callSignal = notification.callSignal;
final long timeStamp = notification.t;

FcmLoggerUtils.logFcmReceived(appContext, msgid);

Expand All @@ -67,7 +68,7 @@ public void processTalkPayload(Map<String, String> payload) {
public void run() {
if (notification.isCallNotification()) {
NotificationManager.displayTalkCallNotification(activityOrServiceContext, appContext, msgid,
eventType, target, username, groupName, message, initistor, receiver);
eventType, target, username, groupName, message, initistor, receiver, timeStamp);
} else {
NotificationManager.displayTalkNotification(activityOrServiceContext, appContext, "0", msgid,
target, username, groupName, message, eventType, nsound, "", "");
Expand Down
4 changes: 2 additions & 2 deletions src/android/actions/RejectCallAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public RejectCallAction(Context context, String callId, String callType, String
@Override
public void run() {
super.run();

notificationManager.cancel(NotificationUtils.generateCallNotificationId(callId));
/*
GROUP
{
Expand Down Expand Up @@ -83,8 +85,6 @@ public void run() {
Log.i(TAG, e.getLocalizedMessage());

saveRejectCallOnError(context, callId);
} finally {
notificationManager.cancel(NotificationUtils.generateCallNotificationId(callId));
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/android/models/PayloadTalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class PayloadTalk {
public String nType;
public String nsound;
public List<String> mention;
public long t;

public String getMsgid() {
return msgid;
Expand Down Expand Up @@ -110,4 +111,11 @@ public void setNsound(String nsound) {
this.nsound = nsound;
}

public long getTimeStamp() {
return t;
}

public void setTimeStamp(long timeStamp) {
this.t = timeStamp;
}
}
46 changes: 44 additions & 2 deletions src/android/notification/NotificationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.apache.cordova.firebase.utils.DateUtils;
import org.apache.cordova.firebase.utils.NotificationUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -299,14 +300,15 @@ synchronized public static void displayTalkNotification(Context activityOrServic

synchronized public static void displayTalkCallNotification(Context activityOrServiceContext, Context appContext, String msgId,
String callEventType, String callId, String name, String groupName, String callType,
String callInitiator, String callReceiver) {
String callInitiator, String callReceiver, long timeStamp) {
Log.i(TAG, "displayCallNotification: \n" +
"msgId: " + msgId + "\n" +
"callId: " + callId + "\n" +
"username: " + name + "\n" +
"groupName: " + groupName + "\n" +
"callInitiator: " + callInitiator + "\n" +
"callReceiver: " + callReceiver + "\n" +
"timeStamp: " + timeStamp + "\n" +
"callType: " + callType);

if(CALL_EVENT_JOINED_SELF.equals(callEventType) || CALL_EVENT_REJECTED_SELF.equals(callEventType)){
Expand All @@ -331,6 +333,14 @@ synchronized public static void displayTalkCallNotification(Context activityOrSe
return;
}

if (timeStamp > 0){
long currentTime = DateUtils.getCorrectedTime(appContext);
if(currentTime > timeStamp + 60){
showMissedCallNotification(appContext, msgId, callId, name, groupName, callType);
return;
}
}

Integer notificationId = NotificationUtils.generateCallNotificationId(callId);
boolean isGroupCall = !TextUtils.isEmpty(groupName);

Expand Down Expand Up @@ -380,6 +390,38 @@ synchronized public static void displayTalkCallNotification(Context activityOrSe
notificationManager.notify(notificationId, notification);
}

private static void showMissedCallNotification(Context context, String msgId, String callId, String name, String groupName, String callType){
Integer notificationId = callId.hashCode();

String channelId = NotificationCreator.defineChannelId(context, "");
String channelName = NotificationCreator.defineChannelName(context, "");
Uri defaultSoundUri = NotificationCreator.defineSoundUri("");

String title = "Missed " + callType + " call";
String text = NotificationCreator.defineCallNotificationTitle(callId, name, groupName);

PendingIntent pendingIntent = NotificationCreator.createNotifPendingIntentTalk(context,
callId, notificationId, "vncEventType", "chat", null);

NotificationCompat.Builder notificationBuilder = NotificationCreator.createNotification(context, channelId, "",
title, text, null, pendingIntent, defaultSoundUri);

NotificationCreator.setNotificationSmallIcon(context, notificationBuilder);
NotificationCreator.setNotificationColor(context, notificationBuilder);

Notification notification = notificationBuilder.build();

NotificationCreator.setNotificationImageRes(context, notification);

android.app.NotificationManager notificationManager = NotificationUtils.getManager(context);

NotificationCreator.createNotificationChannel(notificationManager, channelId, channelName, "");

notificationManager.notify(notificationId, notification);

NotificationUtils.saveNotificationsIdInFile(context, callId, notificationId);
}

private static boolean checkIfNotificationExist(Context appContext, String msgid) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
SharedPreferences.Editor editor = prefs.edit();
Expand Down Expand Up @@ -585,4 +627,4 @@ public static void cancelCallNotification(Context context, String pushCallId) {
}
}
}
}
}
27 changes: 27 additions & 0 deletions src/android/utils/DateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.apache.cordova.firebase.utils;

import android.content.Context;
import android.util.Log;

public class DateUtils {
static final String TAG = "FirebasePlugin.DateUtils";

public static long getCorrectedTime(Context context) {
long currentTime = System.currentTimeMillis() / 1000;
Log.i(TAG, "Device time = " + currentTime);

try {
int timeDiff = SharedPrefsUtils.getInt(context, "time-diff-interval");
Log.i(TAG, "Time diff from shared = " + timeDiff);

if (timeDiff != -1) {
currentTime = currentTime - (timeDiff / 1000);
Log.i(TAG, "Correct time, diff = " + timeDiff + ", correctedTime = " + currentTime);
}
} catch (Exception e){
Log.w(TAG, "Error during parse diff: " + e.getMessage());
}

return currentTime;
}
}