Skip to content

Commit

Permalink
#3 Several registration bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 20, 2014
1 parent d0680f8 commit 3dabb2b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 75 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="com.phonegap.plugins.PushPlugin"
version="2.4.2">
version="2.4.3">

<name>PushPlugin</name>
<author>Bob Easterday</author>
Expand Down
40 changes: 21 additions & 19 deletions src/android/com/plugin/gcm/GCMIntentService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.plugin.gcm;

import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
Expand All @@ -9,11 +14,8 @@
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;
import com.google.android.gcm.GCMBaseIntentService;

@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {
Expand All @@ -27,24 +29,24 @@ public GCMIntentService() {
@Override
public void onRegistered(Context context, String regId) {

Log.v(TAG, "onRegistered: "+ regId);
Log.v(TAG, "onRegistered: " + regId);

JSONObject json;

try
{
try {
json = new JSONObject().put("event", "registered");
json.put("regid", regId);

Log.v(TAG, "onRegistered: " + json.toString());
// EV: added this because it makes a lot of sense
PushPlugin.sendAsyncRegistrationResult(true, regId);

// EV: kept this for backward compatibility
// Send this JSON data to the JavaScript application above EVENT should be set to the msg type
// In this case this is the registration ID
PushPlugin.sendJavascript( json );
PushPlugin.sendJavascript(json);

}
catch( JSONException e)
{
} catch (JSONException e) {
// No message to the user is sent, JSON failed
Log.e(TAG, "onRegistered: JSON exception");
}
Expand All @@ -61,14 +63,12 @@ protected void onMessage(Context context, Intent intent) {

// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null)
{
if (extras != null) {
// if we are in the foreground, just surface the payload, else post it to the statusbar
if (PushPlugin.isInForeground()) {
extras.putBoolean("foreground", true);
PushPlugin.sendExtras(extras);
}
else {
} else {
extras.putBoolean("foreground", false);

// Send a notification if there is a message
Expand All @@ -82,6 +82,7 @@ protected void onMessage(Context context, Intent intent) {
public void createNotification(Context context, Bundle extras)
{
int notId = 0;

try {
notId = Integer.parseInt(extras.getString("notId", "0"));
}
Expand Down Expand Up @@ -114,7 +115,7 @@ public void createNotification(Context context, Bundle extras)
if (extras.getString("defaults") != null) {
try {
defaults = Integer.parseInt(extras.getString("defaults"));
} catch (NumberFormatException e) {}
} catch (NumberFormatException ignored) {}
}

NotificationCompat.Builder mBuilder =
Expand All @@ -140,7 +141,7 @@ public void createNotification(Context context, Bundle extras)
}


mNotificationManager.notify((String) appName, notId, mBuilder.build());
mNotificationManager.notify(appName, notId, mBuilder.build());
}

private static String getAppName(Context context)
Expand All @@ -154,8 +155,9 @@ private static String getAppName(Context context)
}

@Override
public void onError(Context context, String errorId) {
Log.e(TAG, "onError - errorId: " + errorId);
public void onError(Context context, String error) {
Log.e(TAG, "onError: " + error);
PushPlugin.sendAsyncRegistrationResult(false, error);
}

}
111 changes: 62 additions & 49 deletions src/android/com/plugin/gcm/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class PushPlugin extends CordovaPlugin {

private static CordovaWebView gWebView;
private static String gECB;
private static String gSenderID;
private static Bundle gCachedExtras = null;
private static boolean gForeground = false;
private static boolean gForeground = false;
private static CallbackContext rememberedAsyncCallback;

/**
* Gets the application context from cordova's main activity.
Expand All @@ -41,7 +41,7 @@ private Context getApplicationContext() {
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

boolean result = false;
boolean result;

Log.v(TAG, "execute: action=" + action);

Expand All @@ -56,13 +56,18 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
Log.v(TAG, "execute: jo=" + jo.toString());

gECB = (String) jo.get("ecb");
gSenderID = (String) jo.get("senderID");
String gSenderID = (String) jo.get("senderID");

Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID);

// remember this callback instance because we need it when GCMRegistrar.register returns
rememberedAsyncCallback = callbackContext;

GCMRegistrar.register(getApplicationContext(), gSenderID);
result = true;
callbackContext.success();
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
} catch (JSONException e) {
Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
result = false;
Expand All @@ -77,13 +82,13 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo

} else if (ARE_NOTIFICATIONS_ENABLED.equals(action)) {

Log.v(TAG, "ARE_NOTIFICATIONS_ENABLED");
final boolean registered = GCMRegistrar.isRegistered(getApplicationContext());
Log.d(TAG, "areNotificationsEnabled? " + registered);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, registered));
result = true;
Log.v(TAG, "ARE_NOTIFICATIONS_ENABLED");
final boolean registered = GCMRegistrar.isRegistered(getApplicationContext());
Log.d(TAG, "areNotificationsEnabled? " + registered);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, registered));
result = true;

} else if (UNREGISTER.equals(action)) {
} else if (UNREGISTER.equals(action)) {

GCMRegistrar.unregister(getApplicationContext());

Expand All @@ -99,6 +104,14 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
return result;
}

public static void sendAsyncRegistrationResult(boolean ok, String msg) {
if (rememberedAsyncCallback != null) {
PluginResult result = new PluginResult(ok ? PluginResult.Status.OK : PluginResult.Status.ERROR, msg);
rememberedAsyncCallback.sendPluginResult(result);
rememberedAsyncCallback = null;
}
}

/*
* Sends a json object to the client as parameter to a method which is defined in gECB.
*/
Expand Down Expand Up @@ -127,39 +140,39 @@ public static void sendExtras(Bundle extras)
}
}

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
gForeground = true;
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
gForeground = true;
}

@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}

@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
gForeground = true;
}

@Override
public void onDestroy() {
super.onDestroy();
gForeground = false;
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}

@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
gForeground = true;
}

@Override
public void onDestroy() {
super.onDestroy();
gForeground = false;
gECB = null;
gWebView = null;
}
}

/*
* serializes a bundle to JSON.
*/
private static JSONObject convertBundleToJson(Bundle extras)
{
/*
* serializes a bundle to JSON.
*/
private static JSONObject convertBundleToJson(Bundle extras)
{
try
{
JSONObject json;
Expand Down Expand Up @@ -194,7 +207,7 @@ else if (key.equals("coldstart"))
}

if ( value instanceof String ) {
// Try to figure out if the value is another JSON object
// Try to figure out if the value is another JSON object

String strValue = (String)value;
if (strValue.startsWith("{")) {
Expand Down Expand Up @@ -237,15 +250,15 @@ else if (strValue.startsWith("["))
Log.e(TAG, "extrasToJSON: JSON exception");
}
return null;
}
}

public static boolean isInForeground()
{
return gForeground;
}
public static boolean isInForeground()
{
return gForeground;
}

public static boolean isActive()
{
return gWebView != null;
}
public static boolean isActive()
{
return gWebView != null;
}
}
27 changes: 21 additions & 6 deletions src/wp8/PushPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -45,13 +45,24 @@ public void register(string options)
}

SubscribePushChannelEvents(pushChannel);
var result = new RegisterResult

if (pushChannel.ChannelUri == null)
{
ChannelName = this.pushOptions.ChannelName,
Uri = pushChannel.ChannelUri == null ? string.Empty : pushChannel.ChannelUri.ToString()
};
// wait for the channel to become ready
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.KeepCallback = true;
this.DispatchCommandResult(pluginResult, this.CurrentCommandCallbackId);
}
else
{
var result = new RegisterResult
{
ChannelName = this.pushOptions.ChannelName,
Uri = pushChannel.ChannelUri.ToString()
};

this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result));
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result));
}
}

public void unregister(string options)
Expand Down Expand Up @@ -109,6 +120,10 @@ void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArg
ChannelName = this.pushOptions.ChannelName,
Uri = e.ChannelUri.ToString()
};
if (this.CurrentCommandCallbackId != null)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result));
}
this.ExecuteCallback(this.pushOptions.UriChangedCallback, JsonConvert.SerializeObject(result));
}

Expand Down

0 comments on commit 3dabb2b

Please sign in to comment.