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

(feat) define title of the missed call notification #25

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function CallKitService() {
callConnected: execWithPlugin(function(uuid) {
callKit.callConnected(callUUID);
}),
endCall: execWithPlugin(function(notify) {
callKit.endCall(callUUID, notify);
endCall: execWithPlugin(function(notify, contentTitle) {
callKit.endCall(callUUID, notify, contentTitle);
}),
finishRing: execWithPlugin(function() {
callKit.finishRing();
Expand Down Expand Up @@ -165,13 +165,13 @@ to report the system that the outgoing call is connected.
Use

```javascript
callKitService.endCall(uuid, notify);
callKitService.endCall(notify, contentTitle);
```

to let the system know, the call is ended.

* *uuid: String* - Unique identifier of the call. In case of incoming call, it is provided by the `reportIncomingCall` `onSuccess` callback.
* *notify: Boolean* - If `true`, sends a local notification to the system about the missed call.
* *notify: boolean* - If `true`, sends a local notification to the system about the missed call.
* *contentTitle: String* - Title of the notification (will default to `{appName} call missed`).

On android the callscreen should be displayed by the app. Use

Expand Down
31 changes: 18 additions & 13 deletions src/android/CallKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {

@Override
public synchronized boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {

if (action == null) {
return false;
}
Expand Down Expand Up @@ -86,7 +85,7 @@ else if (action.equals("startCall")) {
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}

return true;
}
else if (action.equals("callConnected")) {
Expand All @@ -96,7 +95,7 @@ else if (action.equals("callConnected")) {
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}

return true;
}
else if (action.equals("endCall")) {
Expand Down Expand Up @@ -130,13 +129,15 @@ private synchronized void register(final JSONArray args, final CallbackContext c
Uri ringtoneUri;

int ringtoneID = ctx.getResources().getIdentifier("ringtone","raw", ctx.getPackageName());
if (ringtoneID != 0 ) {

if (ringtoneID != 0) {
ringtoneUri = Uri.parse("android.resource://" + ctx.getPackageName() + "/" + ringtoneID);
} else {
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}

ringtone = RingtoneManager.getRingtone(ctx, ringtoneUri);

if (Build.VERSION.SDK_INT >= 21) {
AudioAttributes aa = new AudioAttributes.Builder()
.setFlags(AudioAttributes.USAGE_NOTIFICATION_RINGTONE | AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST)
Expand All @@ -145,6 +146,7 @@ private synchronized void register(final JSONArray args, final CallbackContext c
} else {
ringtone.setStreamType(RingtoneManager.TYPE_RINGTONE);
}

ringtone.stop();

callbackContext.success();
Expand Down Expand Up @@ -181,7 +183,9 @@ public void run() {
if(wakeLock.isHeld()) {
wakeLock.release();
}

wakeLock.acquire();

try {
boolean vibrate = false;
Uri ringtoneUri;
Expand Down Expand Up @@ -214,9 +218,9 @@ public void run() {

private synchronized void callConnected(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String uuid = args.getString(0);

/* do nothing... */

callbackContext.success();
}

Expand All @@ -231,7 +235,7 @@ private synchronized void startCall(final JSONArray args, final CallbackContext
callbackContext.success(uuid);
}

private void notifyUser(String uuid) {
private void notifyUser(String uuid, String contentTitle) {
String appName;
ApplicationInfo app = null;

Expand All @@ -248,10 +252,11 @@ private void notifyUser(String uuid) {
e.printStackTrace();
}

boolean useDefaultTitle = contentTitle == null || contentTitle.isEmpty();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setContentTitle( appName + " call missed" )
.setContentText( callName )
.setSound( defaultSoundUri );
.setContentTitle(useDefaultTitle ? (appName + " call missed") : contentTitle)
.setContentText(callName)
.setSound(defaultSoundUri);

int resID = context.getResources().getIdentifier("callkit_missed_call", "drawable", cordova.getActivity().getPackageName());
if (resID != 0) {
Expand All @@ -274,14 +279,15 @@ private synchronized void finishRing(final JSONArray args, final CallbackContext
if(ringtone.isPlaying()) {
ringtone.stop();
}
vibrator.cancel();

vibrator.cancel();
callbackContext.success();
}

private synchronized void endCall(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String uuid = args.getString(0);
boolean notify = args.getBoolean(1);
String contentTitle = args.getString(2);

cordova.getActivity().runOnUiThread(new Runnable() {
@Override
Expand All @@ -301,10 +307,9 @@ public void run() {
finishRing(args,callbackContext);

if (notify) {
this.notifyUser(uuid);
this.notifyUser(uuid, contentTitle);
}

callbackContext.success();
}

}
114 changes: 57 additions & 57 deletions www/CallKit.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
var exec = cordova.require('cordova/exec');

var CallKit = function() {
console.log('CallKit instanced');
console.log('CallKit instanced');
};

CallKit.prototype.register = function(callChanged,audioSystem) {
var errorCallback = function() {};
var successCallback = function(obj) {
if (obj && obj.hasOwnProperty('callbackType')) {
if (obj.callbackType == "callChanged") {
/* this is a call changed callback! */
callChanged(obj);
} else if (obj.callbackType == "audioSystem") {
/* this is an audio system callback! */
audioSystem(obj.message);
}
} else {
}
};

exec(successCallback, errorCallback, 'CallKit', 'register' );
var errorCallback = function() {};
var successCallback = function(obj) {
if (obj && obj.hasOwnProperty('callbackType')) {
if (obj.callbackType == "callChanged") {
/* this is a call changed callback! */
callChanged(obj);
} else if (obj.callbackType == "audioSystem") {
/* this is an audio system callback! */
audioSystem(obj.message);
}
} else {
}
};

exec(successCallback, errorCallback, 'CallKit', 'register' );
};

CallKit.prototype.reportIncomingCall = function(name,params,onSuccess) {
var supportsVideo = true;
var supportsGroup = false;
var supportsUngroup = false;
var supportsDTMF = false;
var supportsHold = false;

if (typeof params === "boolean") {
supportsVideo = params;
} else if (typeof params === "object") {
supportsVideo = (params.video === true);
supportsGroup = (params.group === true);
supportsUngroup = (params.ungroup === true);
supportsDTMF = (params.dtmf === true);
supportsHold = (params.hold === true);
}

var errorCallback = function() {};
var successCallback = function(obj) {
onSuccess(obj);
};

exec(successCallback, errorCallback, 'CallKit', 'reportIncomingCall', [name, supportsVideo, supportsGroup, supportsUngroup, supportsDTMF, supportsHold] );
var supportsVideo = true;
var supportsGroup = false;
var supportsUngroup = false;
var supportsDTMF = false;
var supportsHold = false;

if (typeof params === "boolean") {
supportsVideo = params;
} else if (typeof params === "object") {
supportsVideo = (params.video === true);
supportsGroup = (params.group === true);
supportsUngroup = (params.ungroup === true);
supportsDTMF = (params.dtmf === true);
supportsHold = (params.hold === true);
}

var errorCallback = function() {};
var successCallback = function(obj) {
onSuccess(obj);
};

exec(successCallback, errorCallback, 'CallKit', 'reportIncomingCall', [name, supportsVideo, supportsGroup, supportsUngroup, supportsDTMF, supportsHold] );
};

CallKit.prototype.askNotificationPermission = function() {
// TODO: allow user to pass a succes/error callback to know the user's answer
var cb = function() {};
exec(cb, cb, 'CallKit', 'askNotificationPermission', []);
// TODO: allow user to pass a succes/error callback to know the user's answer
var cb = function() {};
exec(cb, cb, 'CallKit', 'askNotificationPermission', []);
};

CallKit.prototype.startCall = function(name,isVideo,onSuccess) {
var errorCallback = function() {};
var successCallback = function(obj) {
onSuccess(obj);
};
var errorCallback = function() {};
var successCallback = function(obj) {
onSuccess(obj);
};

exec(successCallback, errorCallback, 'CallKit', 'startCall', [name, isVideo] );
exec(successCallback, errorCallback, 'CallKit', 'startCall', [name, isVideo] );
};

CallKit.prototype.callConnected = function(uuid) {
var errorCallback = function() {};
var successCallback = function() {};
var errorCallback = function() {};
var successCallback = function() {};

exec(successCallback, errorCallback, 'CallKit', 'callConnected', [uuid] );
exec(successCallback, errorCallback, 'CallKit', 'callConnected', [uuid] );
};

CallKit.prototype.endCall = function(uuid,notify) {
var errorCallback = function() {};
var successCallback = function() {};
CallKit.prototype.endCall = function(uuid, notify, contentTitle) {
var errorCallback = function() {};
var successCallback = function() {};

exec(successCallback, errorCallback, 'CallKit', 'endCall', [uuid, notify] );
exec(successCallback, errorCallback, 'CallKit', 'endCall', [uuid, notify, contentTitle] );
};

CallKit.prototype.finishRing = function(uuid) {
var errorCallback = function() {};
var successCallback = function() {};
var errorCallback = function() {};
var successCallback = function() {};

exec(successCallback, errorCallback, 'CallKit', 'finishRing', [uuid] );
exec(successCallback, errorCallback, 'CallKit', 'finishRing', [uuid] );
};

if (typeof module != 'undefined' && module.exports) {
module.exports = CallKit;
module.exports = CallKit;
}