Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Support android shortcut badges on supported devices when app is closed #703

Closed
wants to merge 6 commits into from
Closed
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
31 changes: 31 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2013-2016 by appPlant UG. All rights reserved.
*
* @APPPLANT_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apache License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://opensource.org/licenses/Apache-2.0/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPPLANT_LICENSE_HEADER_END@
*/

repositories {
mavenCentral()
}

dependencies {
compile 'me.leolin:ShortcutBadger:1.1.4@aar'
}

3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<engines>
<engine name="cordova" version=">=3.6.3" />
<engine name="cordova-android" version=">=4.0.0" />
<engine name="cordova-ios" version=">=4.0.0" />
<engine name="cordova-ios" version=">=3.9.2" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, for version 1.6.0 and greater of this plugin we'll only support cordova-ios 4.0.0 or greater. Please revert line.

</engines>

<preference name="SENDER_ID" />
Expand Down Expand Up @@ -86,6 +86,7 @@

<framework src="com.android.support:support-v13:23+" />
<framework src="com.google.android.gms:play-services-gcm:+" />
<framework src="build.gradle" custom="true" type="gradleReference" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of including a custom build.gradle file why not use a framework tag:

<framework src="me.leolin:ShortcutBadger:1.1.4@aar" />


<source-file src="src/android/com/adobe/phonegap/push/GCMIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/" />
Expand Down
19 changes: 18 additions & 1 deletion src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import me.leolin.shortcutbadger.ShortcutBadger;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -57,7 +59,6 @@ public void setNotification(int notId, String message){
@Override
public void onMessageReceived(String from, Bundle extras) {
Log.d(LOG_TAG, "onMessage - from: " + from);

if (extras != null) {

SharedPreferences prefs = getApplicationContext().getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
Expand Down Expand Up @@ -191,16 +192,32 @@ private Bundle normalizeExtras(Bundle extras) {
return newExtras;
}

private void updateBadge(Context context, String badge) {

int count = badge == null ? 0 : Integer.parseInt(badge);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make it impossible to keep the badge count unchanged (eg. when it's not set)


if (count > 0) {
ShortcutBadger.applyCount(context, count);
} else if (count == 0) {
ShortcutBadger.removeCount(context);
}
}


private void showNotificationIfPossible (Context context, Bundle extras) {

// Send a notification if there is a message or title, otherwise just send data
String message = extras.getString(MESSAGE);
String title = extras.getString(TITLE);
String contentAvailable = extras.getString(CONTENT_AVAILABLE);
String badgeCount = extras.getString(COUNT);

Log.d(LOG_TAG, "message =[" + message + "]");
Log.d(LOG_TAG, "title =[" + title + "]");
Log.d(LOG_TAG, "contentAvailable =[" + contentAvailable + "]");
Log.d(LOG_TAG, "badgeCount =[" + badgeCount + "]");

updateBadge(context, badgeCount);

if ((message != null && message.length() != 0) ||
(title != null && title.length() != 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import android.util.Log;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused import.

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.iid.InstanceID;

Expand Down