Skip to content

Commit

Permalink
Add notifications when an incoming call appears
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflinwood committed Jul 20, 2013
1 parent 7af7504 commit e6c9bdd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Android/src/com/phonegap/plugins/twilioclient/TCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
Expand All @@ -13,11 +12,16 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

Expand Down Expand Up @@ -48,6 +52,10 @@ public class TCPlugin extends CordovaPlugin implements DeviceListener,
private Connection mConnection;
private CallbackContext mInitCallbackContext;
private JSONArray mInitDeviceSetupArgs;
private int mCurrentNotificationId = 1;
private String mCurrentNotificationText;



private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -59,6 +67,29 @@ public void onReceive(Context context, Intent intent) {
JSONObject connection = new JSONObject();
try {
connection.putOpt("parameters", getJSONObject(mConnection.getParameters()));

if (mCurrentNotificationText != null) {


PackageManager pm = context.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(context.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.btn_star_big_on)
.setContentTitle("Answer")
.setContentText(mCurrentNotificationText)
.setContentIntent(pendingIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) TCPlugin.this.webView.getContext().getSystemService(Activity.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mCurrentNotificationId, mBuilder.build());
}

} catch (JSONException e) {
Log.e(TAG,e.getLocalizedMessage(), e);
}
Expand Down Expand Up @@ -121,6 +152,14 @@ public boolean execute(final String action, final JSONArray args,
} else if ("connectionStatus".equals(action)) {
connectionStatus(callbackContext);
return true;
} else if ("rejectConnection".equals(action)) {
return true;
} else if ("showNotification".equals(action)) {
showNotification(args,callbackContext);
return true;
} else if ("cancelNotification".equals(action)) {
cancelNotification(args,callbackContext);
return true;
}

return false;
Expand Down Expand Up @@ -296,6 +335,21 @@ private void connectionStatus(CallbackContext callbackContext) {
}


private void showNotification(JSONArray arguments, CallbackContext context) {
NotificationManager mNotifyMgr =
(NotificationManager) TCPlugin.this.webView.getContext().getSystemService(Activity.NOTIFICATION_SERVICE);
mNotifyMgr.cancelAll();
mCurrentNotificationText = arguments.optString(0);
context.success();
}

private void cancelNotification(JSONArray arguments, CallbackContext context) {
NotificationManager mNotifyMgr =
(NotificationManager) TCPlugin.this.webView.getContext().getSystemService(Activity.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(mCurrentNotificationId);
context.success();
}

// DeviceListener methods

@Override
Expand Down
12 changes: 12 additions & 0 deletions Android/src/com/phonegap/plugins/twilioclient/tcPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
}
}

TwilioPlugin.Connection.prototype.showNotification = function(alertBody, ringSound) {
var args = [alertBody, ringSound];
if(ringSound === "undefined") {
args = [alertBody];
}
Cordova.exec(null, null, "TCPlugin", "showNotification", args);
}

TwilioPlugin.Connection.prototype.cancelNotification = function() {
Cordova.exec(null, null, "TCPlugin", "cancelNotification", []);
}

TwilioPlugin.Connection.prototype.reject = function() {
Cordova.exec(null,null,"TCPlugin","rejectConnection",[]);
}
Expand Down

0 comments on commit e6c9bdd

Please sign in to comment.