Skip to content

Commit

Permalink
Merge pull request avivais#94 from avilasolucoes-naora/master
Browse files Browse the repository at this point in the history
Solved Problem avivais#90
  • Loading branch information
taivo authored Mar 20, 2017
2 parents bf343b1 + 899f431 commit 2321b45
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 42 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<framework src="com.parse:parse-android:1.13.0" />
<framework src="com.parse.bolts:bolts-android:1.4.0" />
<framework src="com.android.support:support-v4:+" />
<framework src="me.leolin:ShortcutBadger:1.1.13@aar" />

<source-file src="src/android/ParsePushPlugin.java" target-dir="src/github/taivo/parsepushplugin" />
<source-file src="src/android/ParsePushPluginReceiver.java" target-dir="src/github/taivo/parsepushplugin" />
Expand Down
20 changes: 16 additions & 4 deletions src/android/ParsePushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ParsePushPlugin extends CordovaPlugin {

private static CordovaWebView gWebView;
private static boolean gForeground = false;
private static boolean helperPause = false;

public static final String LOGTAG = "ParsePushPlugin";

Expand Down Expand Up @@ -137,13 +138,21 @@ public static void jsCallback(JSONObject _json){
jsCallback(_json, "RECEIVE");
}

public static void jsCallback(JSONObject _json, String pushAction){
public static void jsCallback(JSONObject _json, String pushAction){
List<PluginResult> cbParams = new ArrayList<PluginResult>();
cbParams.add(new PluginResult(PluginResult.Status.OK, _json));
cbParams.add(new PluginResult(PluginResult.Status.OK, pushAction));

PluginResult dataResult = new PluginResult(PluginResult.Status.OK, cbParams);
dataResult.setKeepCallback(true);
//avoid blank
PluginResult dataResult;
if (pushAction.equals("OPEN")) {
if (helperPause)
dataResult = new PluginResult(PluginResult.Status.OK, _json);
else
dataResult = new PluginResult(PluginResult.Status.OK, cbParams);
} else {
dataResult = new PluginResult(PluginResult.Status.OK, _json);
}
dataResult.setKeepCallback(true);


if(gEventCallback != null){
Expand All @@ -159,6 +168,7 @@ public static void jsCallback(JSONObject _json, String pushAction){
}
}


private static void flushPNQueue(){
while(!pnQueue.isEmpty() && gEventCallback != null){
gEventCallback.sendPluginResult(pnQueue.remove());
Expand All @@ -175,6 +185,7 @@ protected void pluginInitialize() {
public void onPause(boolean multitasking) {
super.onPause(multitasking);
gForeground = false;
helperPause = true;
}

@Override
Expand All @@ -189,6 +200,7 @@ public void onDestroy() {
gWebView = null;
gForeground = false;
gEventCallback = null;
helperPause = false;

super.onDestroy();
}
Expand Down
75 changes: 39 additions & 36 deletions src/android/ParsePushPluginReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
import java.util.List;
import java.util.Random;

import android.content.SharedPreferences;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import me.leolin.shortcutbadger.ShortcutBadger;


public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver
{
Expand All @@ -36,6 +44,8 @@ public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver
private static JSONObject MSG_COUNTS = new JSONObject();
private static int badgeCount = 0;

private static final String KEY = "badge";


@Override
protected void onPushReceive(Context context, Intent intent) {
Expand Down Expand Up @@ -167,7 +177,7 @@ protected Notification getNotification(Context context, Intent intent){
Log.e(LOGTAG, "JSONException while parsing badge:", e);
}

setBadge(context, badgeCount);
setBadge(badgeCount, context);
}

builder.setSmallIcon(getSmallIconId(context, intent))
Expand Down Expand Up @@ -232,44 +242,37 @@ private static void resetCount(String pnTag){
* Badge Counter methods. This will display badge counters on Samsung and Sony launchers.
*/

public static void resetBadge(Context context) {
badgeCount = 0;
setBadgeSamsung(context, 0);
setBadgeSony(context, 0);
}

public static void setBadge(Context context, int count) {
setBadgeSamsung(context, count);
setBadgeSony(context, count);
}

public static void setBadgeSamsung(Context context, int count) {
String launcherClassName = getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
Log.d("PushReceiver", "Samsung: "+context.getPackageName()+" "+launcherClassName);
}

public static void setBadgeSony(Context context, int count) {
String launcherClassName = getLauncherClassName(context);
/**
* Sets the badge of the app icon.
*
* @param args
* The new badge number
* @param ctx
* The application context
*/
public static void setBadge (int badgeCount, Context ctx) {
int badge = badgeCount;

saveBadge(badge, ctx);
ShortcutBadger.applyCount(ctx, badge);
}

public static void resetBadge (Context ctx) {
saveBadge(0, ctx);
ShortcutBadger.removeCount(ctx);
}

Intent intent = new Intent();
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", count>0);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", ""+count);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
private static void saveBadge (int badge, Context ctx) {
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();

context.sendBroadcast(intent);
Log.d("PushReceiver", "Sony: " + context.getPackageName() + " " + launcherClassName);
}
editor.putInt(KEY, badge);
editor.apply();
}

private static SharedPreferences getSharedPreferences (Context context) {
return context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
}

public static String getLauncherClassName(Context context) {

PackageManager pm = context.getPackageManager();
Expand Down
9 changes: 9 additions & 0 deletions src/android/parse-push-plugin.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


repositories {
mavenCentral()
}

dependencies {
compile 'me.leolin:ShortcutBadger:1.1.11@aar'
}
4 changes: 2 additions & 2 deletions www/parse-push-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require('cordova/channel').onCordovaReady.subscribe(function() {
}

if(pn !== null){
if(pushAction === 'OPEN'){
if(pushAction === 'OPEN' || pn.OPEN){
//
// trigger a callback when user click open a notification.
// One usecase for this pertains a cordova app that is already running in the background.
Expand Down Expand Up @@ -93,7 +93,7 @@ function poorManExtend(object, source){
var eventSplitter = /\s+/;
var slice = Array.prototype.slice;
var EventMixin = {
_coldStartDelayMs: 300,
_coldStartDelayMs: 1000,
on: function(events, callback, context) {

var calls, event, node, tail, list;
Expand Down

0 comments on commit 2321b45

Please sign in to comment.